node how to set environment variable

Setting Environment Variable in Node.js

To set an environment variable in Node.js, follow these steps:

  1. Using process.env:
  2. Use the process.env object to access environment variables in Node.js. You can set a new environment variable by assigning a value to a new key in the process.env object.
  3. Example: process.env.NEW_VARIABLE = 'new value';

[[SOURCE 4]]

  1. Using Command Line:
  2. You can also set environment variables using the command line when running your Node.js application.
  3. Example: NEW_VARIABLE=new_value node app.js

[[SOURCE 4]]

  1. Using .env File:
  2. Create a .env file in the root directory of your Node.js application.
  3. Add environment variables in the .env file in the format KEY=VALUE.
  4. Example: NEW_VARIABLE=new_value

[[SOURCE 4]]

By following these steps, you can effectively set environment variables in your Node.js applications.