This repository has been archived by the owner. It is now read-only.

NPM_CONFIG_PREFIX does not work inside a bash script ran via an npm-script #14528

Closed
villelahdenvuo opened this Issue Nov 4, 2016 · 4 comments

Comments

Projects
None yet
4 participants
@villelahdenvuo
Contributor

villelahdenvuo commented Nov 4, 2016

I'm opening this issue because:

  • npm is crashing.
  • npm is producing an incorrect install.
  • npm is doing something I don't understand.
  • Other (see below for feature requests):

What's going wrong?

Using the environment variable NPM_CONFIG_PREFIX does not work if ran inside a bash script via npm-script.

How can the CLI team reproduce the problem?

test.sh

#!/bin/bash
NPM_CONFIG_PREFIX=/tmp/test npm config get prefix
npm_config_prefix=/tmp/test npm config get prefix

package.json

{
  "name": "npm-config-test",
  "scripts": {
    "test": "./test.sh",
    "test2": "NPM_CONFIG_PREFIX=/tmp/test npm config get prefix && npm_config_prefix=/tmp/test npm config get prefix",
    "test3": "NPM_CONFIG_PREFIX=/tmp/test ./test.sh",
    "test4": "npm_config_prefix=/tmp/test ./test.sh"
  }
}

Running ./test.sh directly works

➜  npm-config-test ./test.sh
/tmp/test
/tmp/test

Running it through an npm script fails

➜  npm-config-test npm test

> npm-config-test@1.0.0 test /home/$USER/npm-config-test
> ./test.sh

/home/$USER/n
/tmp/test

Running directly in an npm script works

➜  npm-config-test npm run test2

> npm-config-test@1.0.0 test2 /home/$USER/npm-config-test
> NPM_CONFIG_PREFIX=/tmp/test npm config get prefix && npm_config_prefix=/tmp/test npm config get prefix

/tmp/test
/tmp/test

Setting the variable uppercase before the script execution fails

➜  npm-config-test npm run test3

> npm-config-test@1.0.0 test3 /home/$USER/npm-config-test
> NPM_CONFIG_PREFIX=/tmp/test ./test.sh

/home/$USER/n
/tmp/test

Setting the variable lowercase before the script execution works

➜  npm-config-test npm run test4

> npm-config-test@1.0.0 test4 /home/$USER/npm-config-test
> npm_config_prefix=/tmp/test ./test.sh

/tmp/test
/tmp/test

supporting information:

  • npm -v prints: 3.10.8
  • node -v prints: v7.0.0
  • npm config get registry prints: https://registry.npmjs.org/
  • Windows, OS X/macOS, or Linux?: Linux/Ubuntu
  • Container:
    • I develop using Vagrant on Windows.
    • I develop using Vagrant on OS X or Linux.
    • I develop / deploy using Docker.
      • Issue originally happened in docker-node, but it also happens on the host
    • I deploy to a PaaS (Triton, Heroku).
@legodude17

This comment has been minimized.

Show comment
Hide comment
@legodude17

legodude17 Nov 4, 2016

Contributor

This is very strange, but probably has to do with how the lifecycle env is assembled in makeEnv.

Contributor

legodude17 commented Nov 4, 2016

This is very strange, but probably has to do with how the lifecycle env is assembled in makeEnv.

@zkat

This comment has been minimized.

Show comment
Hide comment
@zkat

zkat Nov 4, 2016

Member

Hey, @tuhoojabotti! In response to the comments in npm/docs#768, I think this could definitely be improved in our docs, and we'd be more than happy to merge a clarification of doc/misc/npm-config.md for that.

The reason you're seeing this behavior is a mixture of Node's own case-folding logic for environment variables (where it will prefer lowercase versions when both casings are present), and the fact that npm itself sets the lowercase version of all these configurations itself, effectively trumping external user configuration. It's a tricky scenario, but that's the way the cookie crumbles, I guess. :) The way you're setting env variables, btw, will not really work on Windows. Check out cross-env for a package that will help with that!

Thanks for the submission! I'm closing this issue with patches-welcome because a doc PR would, as I said before, be more than welcome.

Member

zkat commented Nov 4, 2016

Hey, @tuhoojabotti! In response to the comments in npm/docs#768, I think this could definitely be improved in our docs, and we'd be more than happy to merge a clarification of doc/misc/npm-config.md for that.

The reason you're seeing this behavior is a mixture of Node's own case-folding logic for environment variables (where it will prefer lowercase versions when both casings are present), and the fact that npm itself sets the lowercase version of all these configurations itself, effectively trumping external user configuration. It's a tricky scenario, but that's the way the cookie crumbles, I guess. :) The way you're setting env variables, btw, will not really work on Windows. Check out cross-env for a package that will help with that!

Thanks for the submission! I'm closing this issue with patches-welcome because a doc PR would, as I said before, be more than welcome.

@zkat zkat closed this Nov 4, 2016

@villelahdenvuo

This comment has been minimized.

Show comment
Hide comment
@villelahdenvuo

villelahdenvuo Nov 4, 2016

Contributor

@zkat thanks for the info! It makes sense now. I know about crossenv, but Windows support is not a problem for my use-case because it is an internal tool. It took me a couple days to track down the problem. At first I thought the problem was Docker. I will work on getting a new pr done.

Contributor

villelahdenvuo commented Nov 4, 2016

@zkat thanks for the info! It makes sense now. I know about crossenv, but Windows support is not a problem for my use-case because it is an internal tool. It took me a couple days to track down the problem. At first I thought the problem was Docker. I will work on getting a new pr done.

@zkat

This comment has been minimized.

Show comment
Hide comment
@zkat

zkat Nov 4, 2016

Member

@tuhoojabotti Yeah! It's super confusing for sure. Thanks so much for taking the time to cook up that PR! ❤️

Member

zkat commented Nov 4, 2016

@tuhoojabotti Yeah! It's super confusing for sure. Thanks so much for taking the time to cook up that PR! ❤️

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.