I know we can declare env variables to be used inside the script run by npm run
command like so:
TEMP_VARIABLE=value node app.js
But if I need to use the declared variable across multiple npm run
scripts, then it will lead to duplicate the effort to specifying the value of variable each time, like in following code example:
"start": "SRC_DIR=src node src/app.js",
"lint": "SRC_DIR=src jshint src/*.js",
"coverage": "SRC_DIR=src istanbul cover --dir outputDir -i src/*.js"
Is there a way so that we can use npm run-script
to export a env variable to allow something like below:
"scripts": {
"set-env": "export SRC_DIR=src", # should export the env var to be used later
"start": "node ${SRC_DIR}/app.js", # use the env var set earlier.
"lint": "jshint ${SRC_DIR}/*.js" # use the same env var again
"coverage": "istanbul cover -d ./lcov -i ${SRC_DIR}/*.js" # use again
}
Then we can just do:
npm run set-env
npm run lint
npm run start
config
object: docs.npmjs.com/files/package.json#config? – Islean$PATH
environment variable. In that case,config
won't work. – Observe