I am using webpack in a Typescript project. I am following a tutorial where I have created 3 webpack files:
webpack.common.jswebpack.production.jswebpack.development.js
Within the tutorial's package.json the "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:
$env:NODE_ENV="development"
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 package.json viz. $NODE_ENV to accept the variables?
Using cross-env
It is possible to achieve something similar using cross-env by doing the following:
npm i --save-dev cross-envWithin
"scripts"add:"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
PowerShell Limitations
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
And & is reserved for future-use.