Reputation: 13507
Dev environment starts with this command:
nodemon -w src --exec \"babel-node src --presets es2015,stage-0\"
How do i create a global variable (or process.env
variable) __DEV__ = true
?
Upvotes: 15
Views: 38696
Reputation: 33
For windows: set __DEV__ = true&&nodemon -w src --exec \"babel-node src --presets es2015,stage-0\
"
Upvotes: 1
Reputation: 1187
If you don't want to handle the env variables in the nodemon call, you can do something like this.
Create a file called '.env' and put something like this in it:
DEV=true
Then in your application entry file put the following line in as early as possible:
require('dotenv').config();
Upvotes: 0
Reputation: 15237
I normally use the dotenv module on my projects.
We just need to create a .env
file and require the dotenv
module in our project:
.env
file:
__DEV__="true"
your-script.js
file:
require('dotenv').config();
console.log(process.env.__DEV__)
Creating .env
files is normally a good option since we can prevent to commit our environment files using .gitignore
Upvotes: 1
Reputation: 387
You can add a "nodemonConfig" property to package.json with your env info. Then execute nodemon in your scripts section.
"nodemonConfig": {
"restartable": "rs",
"ignore": [
"node_modules/**/node_modules"
],
"delay": "2500",
"env": {
"NODE_ENV": "development",
"NODE_CONFIG_DIR": "./config"
}
}
Upvotes: 11
Reputation: 1653
You can either add "env"
property to nodemon.json
, like this:
...
"env": {
"__DEV__": "true"
}
Or you can prepend __DEV__="true"
to start
script in package.json
.
Both worked for me.
Upvotes: 11
Reputation: 982
just define in codes (server file) like this proccess.env.VARIABLE="true"
Upvotes: -3