简体   繁体   English

nodemon 未加载环境变量

[英]nodemon not loading environment variables

The following is in my gulpfile.js :以下是我的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:我希望在package.json文件中复制与dev脚本完全相同的行为:

  "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.对于某种原因似乎gulp不登记在定义的环境变量.env文件。 Although the line below works just fine when ran with npm run dev :虽然下面的行在使用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 :您可能需要将export添加到您的环境变量并在运行nodemon之前加载它们:

vars.env变量文件

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

package.json包.json

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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