【问题标题】:nodemon not loading environment variablesnodemon不加载环境变量
【发布时间】:2020-05-20 04:02:10
【问题描述】:

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

我希望在 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"
  },

出于某种原因,gulp 似乎没有注册.env 文件中定义的环境变量。虽然下面的行在使用npm run dev 运行时工作得很好:

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

【问题讨论】:

  • dotenv_config_path 的值中应该有空格吗?
  • 不,这不应该是复制/粘贴问题

标签: javascript node.js gulp dotenv


【解决方案1】:

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

vars.env

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

package.json

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

【讨论】:

    猜你喜欢
    • 2014-10-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-26
    • 2017-12-08
    • 2019-10-31
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多