Open pereorga opened 10 months ago
There’s no need; set NODE_OPTIONS to contain that node option.
thanks!
Based on @ljharb's comment that it should work with just setting NODE_OPTIONS I expected method 1 and 2 to work, but it only worked with method 3.
// Method 1
{
...,
"scripts": {
"build": "set NODE_OPTIONS='--env-file=.env' && webpack --config webpack.config.js"
},
...
}
Another method I expected to work but didn't:
// Method 2
set NODE_OPTIONS='--env-file=.env'
npm run build
The method that ended up working for me, thank you meduzen in https://github.com/nodejs/node/issues/49514
// Method 3
{
...,
"scripts": {
"build": "node --env-file=.env node_modules/webpack/bin/webpack --config webpack.config.js"
},
...
}
In webpack.config.js I have two console logs: console.log("A .env value:"); console.log(process.env.MY_VAR);
@pereorga @ljharb Did NODE_OPTIONS
work? I am getting: --env-file= is not allowed in NODE_OPTIONS
. It's not listed in the documentation.
There’s no need; set NODE_OPTIONS to contain that node option.
@ljharb can this be reopened? As per comments above, this is not an option. In latest node LTS (20.11.1) and current (21.7.1) you get
node: --env-file is not allowed in NODE_OPTIONS
Also see
Now that process.loadEnvFile
and util.parseEnv
are available, perhaps this can be implemented as a cli flag in npx?
up
Would love to see this
See equivalent Node.js option in https://nodejs.org/dist/latest-v20.x/docs/api/cli.html#--env-fileconfig
Related: https://github.com/nodejs/node/issues/49514