I have an npm script like this that executes two node files:
package.json
{
"scripts": {
"my-target": "node setup.js && node run.js"
}
}
Ideally, I can set environment variables in setup.js that can then be accessed in run.js.
setup.js
process.env.HELLO_WORLD=1
run.js
// Ideally prints `1`, instead prints undefined.
console.log(process.env.HELLO_WORLD);
Is something like this possible? Ideally the result of setting the environment variables in setup.js would be persistent, so that if I run
$ echo $HELLO_WORLD
from bash, that reference would still be there.
You can set env vars when you run the main program:
HELLO_WORLD=hello npm run my-target
package.json:
"scripts": {
"my-target": "node setup.js && node run.js"
},
setup.js:
console.info('setup', process.env.HELLO_WORLD);
run.js:
console.info('run', process.env.HELLO_WORLD);
Output will be
setup hello
run hello