Skip to content

Instantly share code, notes, and snippets.

@berryp
Last active May 17, 2017 16:05
Show Gist options
  • Save berryp/26a7c60c69707a71ff29238e58b3306d to your computer and use it in GitHub Desktop.
Save berryp/26a7c60c69707a71ff29238e58b3306d to your computer and use it in GitHub Desktop.

NPM Artifactory proxy

NPM reads package names from the first slug ion the URL in the request. Artifactory URLs have a bunch of slugs before the package name. This means that when NPM prints out the package name in case of an error, you don’t get the package name when using Artifactory.

This Nginx config will allow you to proxy requests from a naked path on a local Nginx server to the full Artifactory URL.

Setup

Nginx

Create a file in the current directory called nginx.conf (or any name of your choice, just make sure you use than name in all instructions) and add the following:

events {
  worker_connections  1024;
}

http {
  server {
    listen 8080;
    server_name localhost;

    location ~ /(?<path>.*) {
      proxy_redirect     off;
      rewrite /(.*) /artifactory/api/npm/npm/$1 break;
      proxy_pass https://your-artifactory-host;
    }
  }
}

NPM

Edit the .npmrc file in your Node project's root (create it if it doesn't exist), and set the registry to the local Nginx server as follows:

registry=http://localhost:8080

Usage

$ nginx -c $PWD/nginx.conf

Then run your NPM installs in the usual way. To kill Nginx when you are finished just run nginx -s stop.

#code #toolbox #node

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