当 NODE_ENV 设置为暂存时,Heroku PG 数据库迁移默认为生产

Heroku PG Database migration defaulting to production when NODE_ENV is set to staging

我的应用程序使用 Express、Knex 和 PG 库。我已经在 Heroku 中设置我的 NODE_ENV 以使用命令 https://devcenter.heroku.com/articles/nodejs-support#runtime-behavior (source):

进行暂存
Setting NODE_ENV and restarting    
NODE_ENV: staging

我在 knexfile 中有一个暂存环境:

require('dotenv').config();

module.exports = {
    test: {
        client: 'pg',
        connection: process.env.DB_URL_TEST,
        migrations: {
            directory: './db/migrations',
        },
        seeds: {
            directory: './db/seeds/dev',
        },
        useNullAsDefault: true,
    },
    development: {
        client: 'pg',
        connection: process.env.DB_URL,
        migrations: {
            directory: './db/migrations',
        },
        seeds: {
            directory: './db/seeds/dev',
        },
        useNullAsDefault: true,
    },

    staging: {
        client: 'pg',
        connection: process.env.DATABASE_URL,
        migrations: {
            directory: './db/migrations',
        },
        seeds: {
            directory: './db/seeds/dev',
        },
        useNullAsDefault: true,
    },

    production: {
        client: 'pg',
        connection: process.env.DB_URL_PRODUCTION,
        migrations: {
            directory: './db/migrations',
        },
        seeds: {
            directory: './db/seeds/production',
        },
        useNullAsDefault: true,
    },
};

我的数据库配置文件监听 NODE_ENV:

    const knex = require('knex');
const config = require('../knexfile');

const dbEnv = process.env.NODE_ENV;

module.exports = knex(config[dbEnv]);

Heroku 中没有其他配置变量:

当我 运行 进行 heroku 迁移时,它一直默认为生产环境:

Running knex migrate:latest
Using environment: production

我发现强制迁移到 staging env 的唯一方法是将 NODE_ENV 显式设置为 staging 作为 Heroku env 变量。我不明白为什么在 运行 迁移时没有选择暂存环境。

编辑:将第一个和第三个屏幕截图转换为文本。其他的真的改不了,只是截图了一部分Heroku环境变量。

** 编辑 2**:添加了将 NODE_ENV 设置为暂存

的命令

与 Heroku 客户支持聊天,在下面找到他们解决了我的问题的答案:

This doesn't come from NODE_ENV but from a side-effect of setting NPM_CONFIG_PRODUCTION=true. Refer this article for details.

Set to true to run in "production" mode.

devDependencies are not installed at the topmost level when running local npm install without any arguments. Set the NODE_ENV="production" for lifecycle scripts. This behavior is non-intuitive, so we try to warn about it in the build log:

-----> Creating runtime environment

   NPM_CONFIG_LOGLEVEL=error
   NPM_CONFIG_PRODUCTION=true
   NODE_VERBOSE=false
   NODE_ENV=staging
   NODE_MODULES_CACHE=true

   npm scripts will see NODE_ENV=production (not 'staging')
   https://docs.npmjs.com/misc/config#production The functionality of user-env-compile is now built into the standard build process.

NODE_ENV is not set by default on Heroku. What you're seeing is the result of NPM_CONFIG_PRODUCTION during build. That is true by default during builds (it dramatically speeds them up by not installing devDependencies). Npm sets NODE_ENV to production whenever NPM_CONFIG_PRODUCTION is true:

-----> Node.js app detected

-----> Reading application state package.json... build directory... cache directory... environment variables...

   Node engine:         0.10.x
   Npm engine:          unspecified
   Start mechanism:     Procfile
   node_modules source: package.json
   node_modules cached: true

   NPM_CONFIG_PRODUCTION=true
   NODE_MODULES_CACHE=true If you'd like to disable production mode for npm, you can set NPM_CONFIG_PRODUCTION to false using the

command heroku config:set NPM_CONFIG_PRODUCTION=false -a This will prevent npm from overriding NODE_ENV.