Local npm registry mirror

The npm registry can be slow at times. It's using a free hosted service so the hardware is probably a bit overloaded. One option to speed it up is to have a local mirror. In theory that should be easily possible because the registry uses couchdb which has this really nice replication feature built in. But in practice theory doesn't work. So here is a step-by-step guide how to set up the mirror. At the end of it you will have a complete mirror of the npm registry.

Start by installing couchdb. Before you start it, you need to change an option in its config file (if you used homebrew then the config file is here: /usr/local/etc/couchdb/local.ini). Disable secure rewrites:

secure_rewrites = false

Now you can start couchdb (by whatever means appropriate), create an admin user and the registry database. I'm using admin as the username and secret as the password. You don't have to use highly secure credentials as it's just your local mirror. However if you're setting up a public mirror you might want to choose a more secure password.

curl -X PUT http://localhost:5984/_config/admins/admin -d '"secret"'
curl -X PUT http://admin:secret@localhost:5984/registry

Because couchdb 1.1.1 has a bug in its replication code (should be fixed in 1.2.0), we'll use a special tool to replicate the database. The first time you run this command it will take a long time to finish. You will also need to run the same command every time you want to update your local mirror, though the incremental updates will be much faster. While it's replicating the database, you can continue with the other steps below.

npm install -g replicate
replicate http://isaacs.iriscouch.com/registry \

While this tool is replicating the database you can install the couchapp. That is a webapp which implements the registry API. It's as simple as cloning its repository and loading it into couchdb.

git clone https://github.com/isaacs/npmjs.org.git && cd npmjs.org

npm install couchapp semver
./node_modules/.bin/couchapp push registry/app.js \

After the replicate command finishes, you will have a complete mirror of the registry in your local couchdb. Use the --registry option to tell npm to contact your local mirror. Installing packages will be much faster than before.

npm --registry http://localhost:5984/registry/_design/app/_rewrite \
    install yourpackage