简体   繁体   中英

nodemon not loading environment variables

The following is in my gulpfile.js :

const nodemon = require('nodemon')

const server = nodemon({
    script: './src/index.js',
    ext: 'hbs json js css',
    require: 'dotenv/config',
    dotenv_config_path: './config/.env',
    stdout: false // without this line the stdout event won't fire
})

I am looking to duplicate the exact same behavior as the dev script in the package.json file:

  "scripts": {
    "test": "jest --watch",
    "dev": "nodemon -r dotenv/config ./src/index.js dotenv_config_path=./config/.env -e js,hbs,json,css",
    "debug": "nodemon --inspect -r dotenv/config ./src/index.js dotenv_config_path=./config/.env -e js,hbs,json,css"
  },

For one reason or another it seems that gulp is not registering the environment variables defined in the .env file. Although the line below works just fine when ran with npm run dev :

nodemon -r dotenv/config ./src/index.js dotenv_config_path=./config/.env -e js,hbs,json,css

You may need to add export to your environment vars and load them before running nodemon :

vars.env

export ENV_FIRST="J"
export ENV_LAST="Doe"

package.json

"scripts": {
  "dev": "source vars.env; nodemon ..."
  ...
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM