zkat / npx

execute npm package binaries (moved)

Home Page:https://github.com/npm/npx

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Global npmrc config not respected

franjohn21 opened this issue · comments

Versions

node version 8.11.2
npm version 6.1.0

Bug information:

We use a custom registry and leverage the global config file for our npmrc located at /etc/npmrc. We don't have a user config located at $HOME/.npmrc. For all other npm operations this works fine and the global config is respected.

npx however does not seem to respect the global config npmrc file and thus it does not use our custom registry

Steps to reproduce

  • Set a bogus registry in your global config.
# /etc/npmrc
registry=http://abc
  • Move your local user config in $HOME/.npmrc if you have one:
    cp ~/.npmrc ~/.npmrc-bak

  • Try installing something via npx. This command should not work but it does.

francis-john:~$ npx cowsay asdf
npx: installed 10 in 1.556s
 _____
< asdf >
 -----
        \   ^__^
         \  (oo)\_______
            (__)\       )\/\
                ||----w |
                ||     ||
  • You can confirm the global config is not being respected but the user config is by moving the global config to your user config and now the command fails:
# ~/.npmrc
registry=http://abc
francis-john:~$ npx cowsay asdf      
npm ERR! code ENOTFOUND
npm ERR! errno ENOTFOUND
npm ERR! network request to http://abc/cowsay failed, reason: getaddrinfo ENOTFOUND abc abc:80
npm ERR! network This is a problem related to network connectivity.
npm ERR! network In most cases you are behind a proxy or have bad network settings.
npm ERR! network 
npm ERR! network If you are behind a proxy, please make sure that the
npm ERR! network 'proxy' config is set properly.  See: 'npm help config'

There's a good chance I'm having the same issue. Using TeamCity across hundreds of engineers and dozens of applications being run not within a user directory, but within the standard /opt/teamcity/buildAgent/temp/agentTemp/... directories. So suddenly a build using npx that behind the curtains uses the --global flag is somehow not finding the global .npmrc. I even forced an .npmrc file into the temporary build agent directory, and it continues to ignore that registry definition, subsequently failing to use our custom registry and failing to find the necessary file.

It's an ongoing investigation.

UPDATE: So, I did a whole mess of digging, I discovered that our TC agents have no global .npmrc and bc npx uses the --global flag, it's ignoring the .npmrc file it's running alongside despite the DX scenario that "I'm using a CLI tool in the context of this app, and in all likelihood want to use the npm config of that app, not my global one"

I'm not expecting it to change, but consider that DX scenario when considering which config to follow bc if there is a local config, it's a CLI tool and should honor the context before going to the global config regardless of it's being installed in some root temp/node_modules dir.

no-global--fail
force-local-config--success

Thanks for all your work on npx and npm <3

commented

Hello,

I wanted to point out the same thing. Our company has an internally hosted registry and we point to it using a project (or user account level) .npmrc file. Moving the file to either ~/ or in the root of a project directory yields no result when trying to use npx:

0 info it worked if it ends with ok
1 verbose cli [ '/Users/my-account/.nvm/versions/node/v8.8.0/bin/node',
1 verbose cli   '/Users/my-account/.nvm/versions/node/v8.8.0/lib/node_modules/npm/bin/npm-cli.js',
1 verbose cli   'install',
1 verbose cli   '@my-custom-org/build-check@0.38.0-alpha.4c6719db',
1 verbose cli   '--global',
1 verbose cli   '--prefix',
1 verbose cli   '/Users/my-account/.npm/_npx/15254',
1 verbose cli   '--loglevel',
1 verbose cli   'error',
1 verbose cli   '--json' ]
2 info using npm@5.4.2
3 info using node@v8.8.0
4 verbose npm-session 3989a4b698770a94
5 silly install loadCurrentTree
6 silly install readGlobalPackageData
7 http fetch GET 404 https://registry.npmjs.org/@my-custom-org%2fbuild-check 799ms
8 silly fetchPackageMetaData error for @my-custom-org/build-check@0.38.0-alpha.4c6719db 404 Not Found: @my-custom-org/build-check@0.38.0-alpha.4c6719db
9 verbose stack Error: 404 Not Found: @my-custom-org/build-check@0.38.0-alpha.4c6719db
9 verbose stack     at fetch.then.res (/Users/my-account/.nvm/versions/node/v8.8.0/lib/node_modules/npm/node_modules/pacote/lib/fetchers/registry/fetch.js:42:19)
9 verbose stack     at tryCatcher (/Users/my-account/.nvm/versions/node/v8.8.0/lib/node_modules/npm/node_modules/bluebird/js/release/util.js:16:23)
9 verbose stack     at Promise._settlePromiseFromHandler (/Users/my-account/.nvm/versions/node/v8.8.0/lib/node_modules/npm/node_modules/bluebird/js/release/promise.js:512:31)
9 verbose stack     at Promise._settlePromise (/Users/my-account/.nvm/versions/node/v8.8.0/lib/node_modules/npm/node_modules/bluebird/js/release/promise.js:569:18)
9 verbose stack     at Promise._settlePromise0 (/Users/my-account/.nvm/versions/node/v8.8.0/lib/node_modules/npm/node_modules/bluebird/js/release/promise.js:614:10)
9 verbose stack     at Promise._settlePromises (/Users/my-account/.nvm/versions/node/v8.8.0/lib/node_modules/npm/node_modules/bluebird/js/release/promise.js:693:18)
9 verbose stack     at Async._drainQueue (/Users/my-account/.nvm/versions/node/v8.8.0/lib/node_modules/npm/node_modules/bluebird/js/release/async.js:133:16)
9 verbose stack     at Async._drainQueues (/Users/my-account/.nvm/versions/node/v8.8.0/lib/node_modules/npm/node_modules/bluebird/js/release/async.js:143:10)
9 verbose stack     at Immediate.Async.drainQueues (/Users/my-account/.nvm/versions/node/v8.8.0/lib/node_modules/npm/node_modules/bluebird/js/release/async.js:17:14)
9 verbose stack     at runCallback (timers.js:785:20)
9 verbose stack     at tryOnImmediate (timers.js:747:5)
9 verbose stack     at processImmediate [as _immediateCallback] (timers.js:718:5)
10 verbose cwd /Users/my-account/dev/my-custom-org/app/react-apps
11 verbose Darwin 18.0.0
12 verbose argv "/Users/my-account/.nvm/versions/node/v8.8.0/bin/node" "/Users/my-account/.nvm/versions/node/v8.8.0/lib/node_modules/npm/bin/npm-cli.js" "install" "@my-custom-org/build-check@0.38.0-alpha.4c6719db" "--global" "--prefix" "/Users/my-account/.npm/_npx/15254" "--loglevel" "error" "--json"
13 verbose node v8.8.0
14 verbose npm  v5.4.2
15 error code E404
16 error 404 Not Found: @my-custom-org/build-check@0.38.0-alpha.4c6719db
17 verbose exit [ 1, true ]

However, using an environment variable works fine:

$ npm_config_registry=https://artifactory.my-company.com/api/npm npx @my-custom-org/build-tool-test@0.37.1-alpha.8260762c
npx: installed 1 in 2.923s
Everything is awesome! 👍 💯 

I agree that npx should read config values from the same npmrc locations as npm does.

https://docs.npmjs.com/files/npmrc

However, I notice that the documentation for "per-project config file" states that "Additionally, this file is not read in global mode, such as when running npm install -g.", which is presumably why it's not being picked up by npx.

Same here. Why is npx not respecting .npmrc?

However, I notice that the documentation for "per-project config file" states that "Additionally, this file is not read in global mode, such as when running npm install -g.", which is presumably why it's not being picked up by npx.

@hubgit does npx work like, do npm i -g then uninstall it after execution?