I want to add some variables to environment variables, but could not find the file which stores these variables.
I checked package.JSon and every folders, but can't find the file storing them.
Where does node.js store it's environment variables?
I want to add some variables to environment variables, but could not find the file which stores these variables.
I checked package.JSon and every folders, but can't find the file storing them.
Where does node.js store it's environment variables?
You can create a .env file in your application folder and define all the environment variables you want to use in the application. Below are sample contents of such a file.
DB_HOST=localhost
DB_USER=root
DB_PASS=123456
Then use the dotenv npm package to import all the variables from the .env file to the node process environment. Then you can access those variables from the process.env object.
require('dotenv').config()
var db = require('db')
db.connect({
host: process.env.DB_HOST,
username: process.env.DB_USER,
password: process.env.DB_PASS
})
You can use a package called cross-env .
https://www.npmjs.com/package/cross-env
The issue with using
$ NODE_ENV=test node yourApp.js
is that this wont work in windows systems.
In order to use cross-env you can do the following
npm install --save-dev cross-env
Add the following to your package.json
{
"scripts": {
"dev": "cross-env NODE_ENV=development node index.js"
}
}
You can run your code by running
npm run dev