【问题标题】:environment variables in herokuheroku中的环境变量
【发布时间】:2018-03-25 18:24:34
【问题描述】:
const firebase = require('firebase');
const devConfig = {
 //config here
};
const prodConfig = {
//config here
};

export const firebaseInit = firebase.initializeApp(process.env.NODE_ENV == 'development' ? devConfig : prodConfig);
export const rootRef = firebase.database().ref();

我有这个 firebase 配置文件。

在我的 procfile 我有:web: NODE_ENV=production node ./src/server/index.js

在我的 webpack 中我有:

plugins: [
    new webpack.DefinePlugin({
        'process.env': {
            'NODE_ENV': JSON.stringify(process.env.NODE_ENV) || '"development"'
        }
    }),

在我的 package.json 中有:

"start": "node src/server/index.js",
"start:production": "NODE_ENV=production ./node_modules/.bin/webpack && node src/server/index.js",
"start:dev": "NODE_ENV=development ./node_modules/.bin/webpack && node src/server/index.js",

在我的 heroku 应用程序设置中,我已将 NODE_ENV 设置为键,并将 production 设置为值

我在推送我的代码后部署了主代码。然而,当应用程序启动时,它仍然在我的控制台中以“开发模式”登录。在生产中启动应用程序我缺少什么?我可以在本地做,而不是在 heroku 上

【问题讨论】:

  • @Mark_M 我已经按照上面所说的做了
  • 我看不到你在哪里打电话给heroku config:set NPM_CONFIG_PRODUCTION = production
  • 它说You can also edit config vars on your app’s settings tab on Dashboard: 这就是我所做的。这不是两个选项之间的选择吗?

标签: javascript reactjs heroku production


【解决方案1】:

不确定这是否能解决您的问题,但希望对您有所帮助。

1) Heroku 是否可以在其中没有“NODE_ENV=production”的 Procfile 之前选择“start”脚本?我不使用Procfile,我只是依赖启动脚本,所以不确定。

2) NODE_ENV=production 是否在 start:production 脚本调用中的 && 之后确定执行?为了确保这一点,可能也值得将其复制并粘贴到节点脚本之前。

3) JSON.stringify 会将空字符串解析为 true,因此最好放置 || JSON.stringify 函数内部的“开发”,如下所示:

'NODE_ENV': JSON.stringify(process.env.NODE_ENV || 'development')

4) '"development"' 周围有单引号和双引号,这将创建带有双引号的字符串,这将是与单词 development 不同的字符串。

【讨论】:

    猜你喜欢
    • 2014-03-16
    • 1970-01-01
    • 2018-08-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-19
    • 2019-07-24
    相关资源
    最近更新 更多