Skip to content
This repository was archived by the owner on Aug 11, 2022. It is now read-only.

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

Closed
1 of 8 tasks
villelahdenvuo opened this issue Nov 4, 2016 · 4 comments
Closed
1 of 8 tasks

Comments

@villelahdenvuo
Copy link
Contributor

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
Copy link
Contributor

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

@zkat
Copy link
Contributor

zkat commented Nov 4, 2016

Hey, @tuhoojabotti! In response to the comments in https://github.com/npm/docs/issues/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 as completed Nov 4, 2016
@villelahdenvuo
Copy link
Contributor Author

@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
Copy link
Contributor

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.
Projects
None yet
Development

No branches or pull requests

4 participants