stkvtflw
stkvtflw

Reputation: 13507

nodemon, babel-node: how to set environment variable?

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

Answers (6)

Ramshad
Ramshad

Reputation: 33

For windows: set __DEV__ = true&&nodemon -w src --exec \"babel-node src --presets es2015,stage-0\"

Upvotes: 1

stevenlacerda
stevenlacerda

Reputation: 1187

If you don't want to handle the env variables in the nodemon call, you can do something like this.

  1. Create a file called '.env' and put something like this in it:

    DEV=true

  2. Then in your application entry file put the following line in as early as possible:

    require('dotenv').config();
    

Upvotes: 0

valdeci
valdeci

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

miken
miken

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

Oleksii Filonenko
Oleksii Filonenko

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

farhadamjady
farhadamjady

Reputation: 982

just define in codes (server file) like this proccess.env.VARIABLE="true"

Upvotes: -3

Related Questions