I am using
webpack in a Typescript project. I am following a tutorial where I have created 3
Within the tutorial’s
"scripts" seconds have the following:
"build": "webpack --config webpack.$NODE_ENV.js"
I have been looking into the following SE Query to set the
NODE_ENV for Windows 10.
Where within PowerShell I perform:
However once I execute
npm run build the script still takes
$NODE_ENV as a string and does not substitute the values.
I might shift to
cross-env later if this doesn’t work for me but I would like to give environments variables a try in the PowerShell.
What should be the equivalent commands for:
NODE_ENV=development npm run build NODE_ENV=production npm run build
in windows and how should I change the
scripts in my
$NODE_ENV to accept the variables?
It is possible to achieve something similar using
cross-env by doing the following:
npm i --save-dev cross-env
"build-dev": "cross-env NODE_ENV=development webpack --config webpack.%NODE_ENV%.js" "build-prod": "cross-env NODE_ENV=production webpack --config webpack.%NODE_ENV%.js"
And this should trigger the respective scripts.
however, this still does not provide flexibility for user to run a generic npm script command whilst setting the env. variable on the fly
There are examples where queries suggest doing something of the likes of:
set NODE_ENV=production&& npm run build
but this fails in PowerShell with the following error:
At line:1 char:24 + set NODE_ENV=production&& npm run build-dev + ~~ The token '&&' is not a valid statement separator in this version. + CategoryInfo : ParserError: (:) , ParentContainsErrorRecordException + FullyQualifiedErrorId : InvalidEndOfLine
& is reserved for future-use.
I was able to Solve this issue based on comment from @ambienBeing. However here are some Caveats:
&&DOES NOT work in the
PowerShellbut WORKS in
$NODE_ENVI needed to adapted
"build": "echo %NODE_ENV% && webpack --config webpack.%NODE_ENV%.js"
Upon executing the following:
set NODE_ENV=production&&npm run build
the script gets executed correctly.
In order to be sure, I added
console.log("production") as well as
console.log("development") in the respective
and I could see the respective string printed in the console twice: 1 x from
echo and 1 x from the file
- This works perfectly well with VS Code when one changes the default shell to