Mindaugas Jačionis Mindaugas Jačionis - 1 month ago 8
Node.js Question

npm run script that passes value to .js file

I am adding these build scripts to my package.json:

npm run do-build --dev
npm run do-build --prod


I want run exec file to do a build for me, but based on these --dev/prod I would like to pass argument to my setEnvironment.js file that would basically set variable if this is dev or prod environment and based on that variable some features will be on/off.

In my package.json in scripts how can I pass variable/value to js file I am calling.

"scripts": {
"do-build:dev": "node ./src/setEnvironment.js"
}


How do pass variable to setEnvironment.js in my do-build script?

Answer

There are multiple ways to do something like that.

The simplest is to define two tasks like that:

"scripts": {
    "do-build:dev": "ENV=DEV node ./src/setEnvironment.js",
    "do-build:prod": "ENV=PROD node ./src/setEnvironment.js"
}