简体   繁体   中英

Dynamic require() on node NODE_ENV with webpack

I am building a simple NodeJs Lambda with webpack. There is a dependent node_module using different configuration file based on NODE_ENV

let config = require(`./${process.env.NODE_ENV ? process.env.NODE_ENV : 'dev'}_env.js`);

I am setting NODE_ENV in package.json

     "serve": "set NODE_ENV=qa&webpack --mode development --watch",
    "serve-windows": "$env:NODE_ENV=qa&webpack --mode development --watch"

and in webpack configuration

mode: process.env.NODE_ENV ? process.env.NODE_ENV : 'default',

No matter what I do, its always including dev_env.js ignoring the NODE_ENV I am setting. I spent all night trying to figure out this looking at different posts, is there anything I am doing incorrectly?

webpack --mode development sets NODE_ENV to development. See the documentation: https://webpack.js.org/configuration/mode/#usage

Another remark, default and qa&webpack are non-standard values for NODE_ENV . Usually, the expected value is either development , test , or production .

If you want to use this approach, you will have to use a different environment variable, for example:

let config = require(`./${process.env.APP_ENV ? process.env.APP_ENV : 'dev'}_env.js`);
"serve": "set APP_ENV=qa&webpack --mode development --watch",

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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