Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I set npm to use a .pac file?

Tags:

node.js

npm

I am trying to set a private registry for npm (nodejs) but I don't want to replicate the entire public database. I have seen posts about how to do this, however, I have another problem even if I follow this approach. My workstation is behind a VPN, so I need to set the proxy in NPM to be able to get modules from the public registry. If I create my private registry, it would sit inside the company VPN (making it publicly accessible is not an option). This means that I do not need the proxy to access my private registry, but like I said before, I do need it 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 issue? I know you can specify registry and proxy when running npm install, but I want to be able to just run npm install. Is there anyway to apply a pac file to npm? Is there something that I can do besides modifying the source code?

like image 406
El Moreno Avatar asked Nov 13 '22 08:11

El Moreno


1 Answers

I faced this exact problem. We setup an nginx proxy in front of the private npm registry. We created a fallback to the global npm registry on a 404.

So when doing an npm install, we just had to specify the nginx proxy, and that would take care of serving the package from the private registry if found, or the global registry if not.

This is the nginx configuration that 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; 
}
like image 73
Ali Avatar answered Nov 14 '22 21:11

Ali