# dotenv

***

**1. Loading Environment Variables from a `.env` File**

```js
require('dotenv').config();

console.log(process.env.MY_VARIABLE);  // Prints the value of the "MY_VARIABLE" environment variable
```

**2. Loading Environment Variables from a Specific File**

```js
require('dotenv').config({ path: './my.env' });

console.log(process.env.MY_VARIABLE);  // Prints the value of the "MY_VARIABLE" environment variable from the "my.env" file
```

**3. Loading Environment Variables for Different Environments**

```js
require('dotenv').config({ path: './.env.' + process.env.NODE_ENV });

console.log(process.env.MY_VARIABLE);  // Prints the value of the "MY_VARIABLE" environment variable for the current environment
```

**4. Loading Environment Variables from Multiple Files**

```js
require('dotenv').config({ path: ['./.env', './my.env'] });

console.log(process.env.MY_VARIABLE);  // Prints the value of the "MY_VARIABLE" environment variable from the first file that contains it
```

**5. Overriding Environment Variables**

```js
process.env.MY_VARIABLE = 'overridden-value';

require('dotenv').config();

console.log(process.env.MY_VARIABLE);  // Prints "overridden-value"
```

**6. Parsing Environment Variables as Numbers**

```js
require('dotenv').config({ parseNumbers: true });

console.log(process.env.MY_NUMBER);  // Automatically parses the value as a number
```

**7. Parsing Environment Variables as Booleans**

```js
require('dotenv').config({ parseBooleans: true });

console.log(process.env.MY_BOOLEAN);  // Automatically parses the value as a boolean
```

**8. Parsing Environment Variables as JSON**

```js
require('dotenv').config({ parseJSON: true, path: './json.env' });

console.log(process.env.MY_OBJECT);  // Parses the value as a JSON object
```

**9. Loading Environment Variables from a Node.js Module**

```js
const dotenv = require('dotenv');

dotenv.config({ module: require('./my-module') });

console.log(process.env.MY_VARIABLE);  // Loads environment variables from the exported object of the "my-module" module
```

**10. Extending the Environment Namespace**

```js
require('dotenv-expand');

require('dotenv').config();

console.log(process.env.MY_VARIABLE);  // Expands any placeholders in the environment variable value
```

**11. Using Environment Variables in Command-Line Arguments**

```js
const dotenv = require('dotenv');

const args = dotenv.parseArgs(process.argv);

console.log(args.MY_VARIABLE);  // Parses environment variables from the command-line arguments
```

**12. Using Environment Variables in a Custom Function**

```js
const dotenv = require('dotenv');

function loadEnvironmentVariables() {
  dotenv.config();
}

loadEnvironmentVariables();

console.log(process.env.MY_VARIABLE);  // Loads environment variables before executing the function
```

**13. Using Environment Variables in a Mocha Test**

```js
const assert = require('assert');
const dotenv = require('dotenv');

describe('Environment variables', function() {
  beforeEach(function() {
    dotenv.config();
  });

  it('should load environment variables', function() {
    assert.equal(process.env.MY_VARIABLE, 'some-value');
  });
});
```

**14. Using Environment Variables in an Express Middleware**

```js
const express = require('express');
const dotenv = require('dotenv');

const app = express();

app.use((req, res, next) => {
  dotenv.config();
  next();
});

app.get('/', (req, res) => {
  res.send(process.env.MY_VARIABLE);  // Responds with the value of the "MY_VARIABLE" environment variable
});
```

**15. Using Environment Variables in a Node.js Service**

```js
const dotenv = require('dotenv');

const service = require('./service');

dotenv.config();

service.start(process.env.MY_PORT);  // Starts the service with the value of the "MY_PORT" environment variable
```

**16. Using Environment Variables in a GraphQL Server**

```js
const express = require('express');
const graphqlHTTP = require('express-graphql');
const dotenv = require('dotenv');

const app = express();

dotenv.config();

app.use('/graphql', graphqlHTTP({
  schema: require('./schema'),
  rootValue: {
    environmentVariable: () => process.env.MY_VARIABLE  // Gets the value of the "MY_VARIABLE" environment variable from the GraphQL API
  }
}));
```

**17. Using Environment Variables in a Webpack Configuration**

```js
const dotenv = require('dotenv');
const webpack = require('webpack');

dotenv.config();

module.exports = {
  plugins: [
    new webpack.DefinePlugin({
      'process.env': {
        MY_VARIABLE: JSON.stringify(process.env.MY_VARIABLE)  // Injects the "MY_VARIABLE" environment variable into the webpack bundle as a JSON string
      }
    })
  ]
};
```

**18. Using Environment Variables in a JavaScript Module**

```js
const dotenv = require('dotenv');

// Load environment variables from a local `.env` file
dotenv.config({ path: './.env' });

module.exports = {
  getEnvironmentVariable: () => process.env.MY_VARIABLE  // Exposes a function to retrieve the "MY_VARIABLE" environment variable
};
```

**19. Using Environment Variables in a React Application**

```js
import dotenv from 'dotenv';

// Load environment variables from a local `.env` file
dotenv.config({ path: './.env' });

const MyComponent = () => {
  const environmentVariable = process.env.MY_VARIABLE;

  return (
    <div>
      {environmentVariable}  {/* Displays the value of the "MY_VARIABLE" environment variable */}
    </div>
  );
};
```

**20. Using Environment Variables in a Vue.js Application**

```js
import dotenv from 'dotenv';

// Load environment variables from a local `.env` file
dotenv.config({ path: './.env' });

const app = new Vue({
  el: '#app',
  data: {
    environmentVariable: process.env.MY_VARIABLE  // Sets a Vue.js data property with the value of the "MY_VARIABLE" environment variable
  }
});
```

**21. Using Environment Variables in an Angular Application**

```js
import dotenv from 'dotenv';

// Load environment variables from a local `.env` file
dotenv.config({ path: './.env' });

@Component({
  selector: 'my-component',
  template: `
    <div>
      {{ environmentVariable }}  {/* Displays the value of the "MY_VARIABLE" environment variable */}
    </div>
  `
})
export class MyComponent {
  public environmentVariable: string = process.env.MY_VARIABLE;
}
```

**22. Using Environment Variables in a Jest Test**

```js
const dotenv = require('dotenv');

// Load environment variables from a local `.env` file
dotenv.config({ path: './.env' });

describe('Environment variables', () => {
  it('should load environment variables', () => {
    expect(process.env.MY_VARIABLE).toBe('some-value');  // Asserts the value of the "MY_VARIABLE" environment variable
  });
});
```

**23. Using Environment Variables in a Cypress Test**

```js
const dotenv = require('dotenv');

// Load environment variables from a local `.env` file
dotenv.config({ path: './.env' });

describe('Environment variables', () => {
  it('should load environment variables', () => {
    cy.visit('/');  // Visits the application's home page
    cy.get('#environment-variable').should('have.text', process.env.MY_VARIABLE);  // Asserts the value of the "MY_VARIABLE" environment variable on the web page
  });
});
```

**24. Using Environment Variables in a Node.js Worker**

```js
const dotenv = require('dotenv');
const cluster = require('cluster');

// Load environment variables from a local `.env` file
dotenv.config({ path: './.env' });

if (cluster.isMaster) {
  // Create worker processes
  for (let i = 0; i < numCPUs; i++) {
    cluster.fork();
  }
} else {
  // Start the worker process
  const environmentVariable = process.env.MY_VARIABLE;

  // Perform some task with the environment variable
}
```

**25. Using Environment Variables in a Dockerfile**

```dockerfile
FROM node:16

ENV MY_VARIABLE=some-value

RUN node server.js  // Start the application with the "MY_VARIABLE" environment variable
```
