node how to set environment variable
Setting Environment Variable in Node.js
To set an environment variable in Node.js, follow these steps:
- Using process.env:
- 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 theprocess.env
object. - Example:
process.env.NEW_VARIABLE = 'new value';
[[SOURCE 4]]
- Using Command Line:
- You can also set environment variables using the command line when running your Node.js application.
- Example:
NEW_VARIABLE=new_value node app.js
[[SOURCE 4]]
- Using .env File:
- Create a
.env
file in the root directory of your Node.js application. - Add environment variables in the
.env
file in the formatKEY=VALUE
. - Example:
NEW_VARIABLE=new_value
[[SOURCE 4]]
By following these steps, you can effectively set environment variables in your Node.js applications.