npm-run-all icon indicating copy to clipboard operation
npm-run-all copied to clipboard

Doesn't work with cross-env

Open wujekbogdan opened this issue 6 years ago • 3 comments

{
  "scripts": {
    "set-env:test": "cross-env NODE_ENV=test",
    "set-env:staging": "cross-env NODE_ENV=staging",
    "set-env:production": "cross-env NODE_ENV=production",
    "deploy:managers": "node ./deployment-scripts/deploy-managers",
    "deploy:managers:test": "npm-run-all set-env:test deploy:managers"
  }
}

When I run deploy:managers:test script I expect the NODE_ENV variable to be set to test for the ./deployment-scripts/deploy-managers.js script. But NODE_ENV is undefined.

If I run the cross-env NODE_ENV=test deploy:managers command then it works as expected.

Is it a bug in npm-run-all or is it me doing something wrong?

wujekbogdan avatar Oct 17 '18 14:10 wujekbogdan

These are two solutions using cross-env-shell which is part of cross-env

"env:prod": "cross-env-shell NODE_ENV=production",
"build:a": "cross-env-shell npm run env:prod npm-run-all a b c"
// or
"build:b": "cross-env-shell NODE_ENV=production npm-run-all a b c"

jagretz avatar Nov 20 '18 16:11 jagretz

@jagretz Thanks, I'll check it out.

wujekbogdan avatar Nov 20 '18 18:11 wujekbogdan

The problem here isn't cross-env but the fact that variables aren't shared across tasks

{
	"set": "HI=hello",
	"use": "echo $HI stranger"
}
$ npm-run-all set use
stranger

To fix this, you have to set the variable before npm-run-all, if possible. For example:

$ HI=hello npm-run-all use
hello stranger

Or in your case

{
    "deploy:managers:test": "cross-env NODE_ENV=test npm run deploy:managers"
}

fregante avatar Jun 07 '19 15:06 fregante