Can I install npm to use a .pac file?

I am trying to install a private registry for npm (nodejs), but I do not want to replicate the entire open database. I have seen posts on how to do this, however I have one more problem, even if I follow this approach. My workstation is behind a VPN, so I need to install a proxy in NPM in order to be able to receive modules from the shared registry. If I create my own private registry, it will sit inside the company's VPN (making it public is not an option). This means that I do not need a proxy to access my private registry, but, as I said, I need this for the public registry. I got the code for NPM from git, but before modifying it, I thought I would just ask, does anyone know how to get around this problem? I know that you can specify the registry and proxy server when running npm install,but I want to be able to run npm install. Is there a way to apply a pac file to npm? Is there anything I can do besides modifying the source code?

+5
source share
1 answer

I ran into this exact problem. We install the nginx proxy in front of the private npm registry. We backed up the global npm 404 registry.

So, when installing npm, we just need to specify the nginx proxy, and this will take care of serving the package from the private registry if it is found, or the global registry if not.

This is the nginx configuration you can use:

server {
    listen 80 default_server;

    location ~ ^/registry/*/ { 
        proxy_set_header  X-Real-IP $remote_addr;
        proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header  Host  $http_host;
        proxy_set_header  X-NginX-Proxy true;

        proxy_pass  http://private_npm_upstream;
        proxy_intercept_errors  on;
        error_page 404 = @fallback-2;

        proxy_redirect off;
    }
    location @fallback-2 {
        access_log /var/log/nginx/global_npm.access.log;

        proxy_set_header  X-Real-IP $remote_addr;
        proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header  Host  registry.npmjs.org;
        proxy_set_header  X-NginX-Proxy true;

        proxy_pass http://registry.npmjs.org;
        proxy_redirect off;
        proxy_intercept_errors  on;
    }
}

upstream global_npm_upstream {
    server registry.npmjs.org;
}

upstream private_npm_upstream { 
    server 127.0.0.1:5984; 
}
+1
source

All Articles