nodemon不加载环境变量

时间:2020-02-04 10:34:45

标签: javascript node.js gulp dotenv

以下是我的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
})

我正在寻找与dev文件中的package.json脚本完全相同的行为:

  "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"
  },

出于某种原因,看来gulp没有注册.env文件中定义的环境变量。尽管下面的行与npm run dev一起运行时效果很好:

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

1 个答案:

答案 0 :(得分:0)

在运行export之前,您可能需要向环境变量中添加nodemon并加载它们:

vars.env

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

package.json

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