Skip to content

Instantly share code, notes, and snippets.

@weshouman
Last active June 9, 2022 08:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save weshouman/2b811a07f091561b34127fe2f5ce38dd to your computer and use it in GitHub Desktop.
Save weshouman/2b811a07f091561b34127fe2f5ce38dd to your computer and use it in GitHub Desktop.
npm common issues

NPM package installation behind a proxy

While installing yo behind proxy you may get issues as described in this issue The root cause is that yo is trying to connect to the internet while doing the sanity checks on your system using a global proxy as described here and there

I was not able to change the GLOBAL_AGENT_ENVIRONMENT_VARIABLE_NAMESPACE from undefined to an empty variable from the powershell, so I edited the node_modules\got\index.js:requestAsEventEmitter(opts):~22 and added the following line

	process.env.GLOBAL_AGENT_ENVIRONMENT_VARIABLE_NAMESPACE="";

Watch Out:

  • pinging the desired address and failing doesn't mean the address is not accessible, it could be very misleading while debugging, but it could be the case that ICMP didn't work for the npm registry.
  • curling should work anyway, also due to the fact that npm registry exists behind cloudflare it is not possible to access through the browser using the IP address, and one needs to use https://registry.npmjs.org/ directly.

Global agent


Edit: By adding the environment variable GLOBAL_AGENT_HTTPS_PROXY set to the PROXY_URL:PROXY_PORT, alongside with the correct NO_PROXY environment variable, for example: localhost,127.0.0.1,10.0.0.0/8,172.0.0.1/12,192.168.0.0/16 the proxy shall work.
Note: In case npm took too much time, like for yo, one needs also to disable the update-notifier by using npm config set update-notifier false


yo uses global-agent which is problematic even with it's interesting concept of masking the proxy to allow having multiple proxy profiles settings per environment each in a different namespace

The problems are:

  • More points of HTTP proxy setting, further debugging for points of failures for the proxy
  • Behavior differs based on the difference between the following values of GLOBAL_AGENT_ENVIRONMENT_VARIABLE_NAMESPACE:
    • undefined: use a GLOBAL_ prefixed http_proxy
    • empty: use the original HTTP_PROXY, HTTPS_PROXY and NO_PROXY env vars
  • Issues show up on different modules with no further hints, which gives interesting side-effects if the modules were not explicit about using this package underhood and theri configurability based on this package's usage, for example, yo-doctor showed it while using got, which internally uses global-agent
  • Under Windows, it's impossible to set an empty environment variable Even when using the command reg add "HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control\Session Manager\Environment" /v GLOBAL_AGENT_ENVIRONMENT_VARIABLE_NAMESPACE /d "" provided in the SO Answer, the env variable would show up in the system env vars, but it would not be retrievable by powershell, cmd, nor even nodejs would catch it.

ESLint

I had some issue with eslint due to a difference in ecmascript version than the one that the code is installed on, that's when I had to update the eslint config file, which is something like that

Edit

Add the environment variables:

  • GLOBAL_AGENT_HTTPS_PROXY set to the PROXY_URL:PROXY_PORT
  • NO_PROXY Disable npm-update-notifier using npm config set update-notifier false

Deprecated

3 packages need fixes for yo generator to be installed and run

Global Agent Fix

As mentioned here at node_modules\yeoman-doctor\lib\rules\yo-version.js
Add the following section

...
exports.verify = async () => {
  try {
    process.env.GLOBAL_AGENT_ENVIRONMENT_VARIABLE_NAMESPACE="";
...

npm-name fix

Do they change found here

inquirer-npm-name fix

Add a catch at the end of the promise found here

      when: function (answers) {
        return npmName(answers[prompt.name]).then(function (available) {
          return !available;
        })
        .catch(()=> return true;})

It should return a false, but currently that freezes nodejs as mentioned here

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