Tags

, , , , , , , ,

I recently decided to setup a local NPM registry for my project. The reasoning behind this endeavour was to solve the following issues.

Issue 1: NPM Install is slow
The first goal of a local NPM registry was to speed up the process of restoring all NPM packages and hopefully decrease the total time a build takes on the continuous integration (CI) server. The CI server completely deletes the build workspace before starting a new build and as a result needs to pull all the NPM packages before starting the build.

Issue 2: NPM is down
In the event that the NPM registry is down, all CI server builds will fail. A local NPM registry will allow the CI server builds to continue in this unlikely, but not unheard of, event.

After a good day of hacking about I was able to stand up the local registry on a Windows server. Here is how I went about it.

Setting up a Local NPM Registry

Prerequisites

Install Chocolatey (http://chocolatey.org)

@powershell -NoProfile -ExecutionPolicy unrestricted -Command "iex ((new-object net.webclient).DownloadString('http://chocolatey.org/install.ps1'))" && SET PATH=%PATH%;%systemdrive%\chocolatey\bin

Install cURL (http://curl.haxx.se)

cinst curl

Install node.js with npm (http://nodejs.org)

cinst nodejs.install

Install CouchDb (http://couchdb.apache.org)

cinst couchdb

Ensure CouchDb is working

curl -X GET http://localhost:5984

If everything is working you should see something like the following:

{"couchdb":"Welcome","version":"1.2.1"}

Update CouchDb configuration
You will need add the following setting to the CouchDb “local.ini” file. You should be able to locate the ini file in C:\Program Files (x86)\Apache Software Foundation\CouchDB\etc\couchdb directory.

[httpd]
secure_rewrites = false

Restart CouchDb

net.exe stop "Apache CouchDB" && net.exe start "Apache CouchDB"

Replicate npmjs.org

Create npmRegistry.json

{"source":"http://isaacs.iriscouch.com/registry", "target":"registry", "continuous":true, "create_target":true}

Start the database replication

curl -X POST http://127.0.0.1:5984/_replicate -d @npmRegistry.json -H "Content-Type: application/json"

Setting up a local NPM

Clone the npm repository

git clone git://github.com/isaacs/npmjs.org.git

Install CouchApp

npm install -g couchapp
npm install couchapp

Install semver

npm install semver

Register npm as a couchdb app

couchapp push registry/app.js http://localhost:5984/registry
couchapp push www/app.js http://localhost:5984/registry

Test the setup

npm --registry http://localhost:5984/registry/_design/scratch/_rewrite login
npm --registry http://localhost:5984/registry/_design/scratch/_rewrite search

Use the new local NPM registry

npm --registry 'registryUrl' install 'packageName'

And there you go! A local NPM registry running on a Windows Server.

Conclusions

After all this I was able to solve only the second issue mentioned above. If the global NPM registry goes down my build scripts do not stop working. Despite what I read online though, I found that the local NPM registry was not faster than the global one. It is so much slower in fact that I have not been able to switch over to it.

If anyone has any idea why this might be please let me know in the comments. Thanks!

Resources:
http://clock.co.uk/tech-blogs/how-to-create-a-private-npmjs-repository
http://wiki.apache.org/couchdb/Quirks_on_Windows
https://github.com/isaacs/npmjs.org