better-npm-run: No longer works as advised

Since https://github.com/benoror/better-npm-run/pull/55 was merged, better-npm-run no longer sets environment from the env field defined in betterScripts if already set by parent process. This breaks several of our tasks since we use better-npm-run specifically to manage environment variables on a per-task basis.

For example, with a script defined as:

"foo": {
  "command": "echo $NODE_ENV",
  "env": {
    "NODE_ENV": "foo"
  }
}

and then run it using:

NODE_ENV=bar npm run foo

the expected behaviour is that NODE_ENV is set to foo, however it is now set to bar.

While I agree with the pull request comment that this behaviour was not clearly documented, from my understanding it’s the whole point of this library, which is now broken.

About this issue

  • Original URL
  • State: closed
  • Created 8 years ago
  • Reactions: 4
  • Comments: 27 (9 by maintainers)

Commits related to this issue

Most upvoted comments

@rochdev update published to NPM as v0.0.13. Thanks again for reporting the issue!

Looks to me like https://github.com/benoror/better-npm-run/pull/59 reverts the behaviour to be as intended, while still supporting @just-boris use case.

@just-boris From your description I think what I’ve proposed earlier would work for your use case without even needing better-npm-run. You could also take a look at how Heroku and similar PaaS are doing it (they all use the PORT variable to configure services)

@just-boris to make sure I understand your use-case, are you saying you want the .env file to override the betterScripts environment variables? Or that the betterScripts environment variables override .env file?

For clarification, if you take the example from the readme.md:

 "betterScripts": {
    "build:dist": "webpack --config $npm_package_webpack --progress --colors",
    "build:prod": {
      "command": "webpack --config $npm_package_webpack --progress --colors",
      "env": {
        "NODE_ENV": "production"
      }
    },

The new behavior now forces NODE_ENV to whatever is in the .env file or the environment which could be development. It makes npm run build:prod effectively broken.