Sometimes it might be a good idea to create a local copy of the npm repository. The following steps enable creation of such a local repository.
The npm repository uses a couchDB database. You’ll need to install couchDB 1.1.0 or higher. For installation details refer here
Specifically note the paragraph on using the new version of couchDB if you already have the earlier version installed. “CouchDB 1.1.1 can be installed on the system alongside 0.10.0. To ensure that they listen on different ports open the file /build-couchdb/build/etc/couchdb/default.ini and modify the following:”
[httpd]
port = 5984 to 5974
Once couchDB is up and running, we need to create our database and populate it.
curl -X PUT http://localhost:5974/registry
Next install couchapp and semver using
npm install couchapp
npm install semver
Sync the registry and search using
couchapp push registry/app.js http://localhost:5984/registry
couchapp push www/app.js http://localhost:5984/registry
To synchronize from the public npm registry to your private registry, three options are available
- Create a replication task from http://isaacs.ic.ht/registry –> local database registry via an HTTP call to ‘/_replicate like so:
curl -X POST -H “Content-Type:application/json” \
http://localhost:5984/_replicate -d \
‘{“source”:”http://isaacs.iriscouch.com/registry/”, “target”:”registry”}’ - Through the CouchBase administrative UI
- npm install replicate -g and then
replicate http://isaacs.iriscouch.com/registry http://localhost:5974/registry.
The size of the registry is around 7.4GB with seq id 62790 after replication.
Once the registry has been replicated, with the setup so far, you can point the npm client at the registry by putting this in your ~/.npmrc file:
registry = http://localhost:5974/registry/_design/app/_rewrite
or
You can also set the npm registry config property like:
npm config set registry http://localhost:5984/registry/_design/app/_rewrite
or you can simple override the registry config on each call:
npm –registry http://localhost:5984/registry/_design/app/_rewrite install
To retore settings just delete the entry in the ~/.npmrc file
References
I really learned about a lot of this, but with that said, I still assumed it had been beneficial. Fine task!
Two things that helped when replication failed:
1. try making requests to single doc_ids (as suggested on #Node.js on freenode):
curl -X POST -H “Content-Type:application/json” http://10.xxx.xxx.xxx:5984/_replicate -d ‘{“source”:”http://isaacs.iriscouch.com/registry/”, “target”:”registry”, “doc_ids”:["underscore"]}’
2. if your registry isn’t syncing try deleting it and giving the ‘create_target’
curl -X POST -H “Content-Type:application/json” http://10.xxx.xxx.xxx:5984/_replicate -d ‘{“source”:”http://isaacs.iriscouch.com/registry/”, “target”:”registry”, “create_target”: true}’
Using doc_ids seems a good option. Had a few incomplete downloads before I could clone the complete registry.
Thank you for this tutorial. As I didn’t even know npm was using couchdb, I learned a lot!
Two questions anyway. When you install couchapp with npm, it cannot be install globally (-g), so how can you run couchapp? what should be put into path (is it just a difference on windows?).
Then I was searching a way to create a private (empty) npm repository for “publishing” modules locally. I don’t want to replicate the whole npm repository. How to achieve that? I want to be able to install a module this way : npm -registry http://localhost:5984 install .
Maybe ist’s documented somewhere else, but didn’t find it…
Thanks.
jg
Thanks for pointing out the requirement of -g in installation of couchapp. Haven’t tried it on Windows, so cannot comment.
What purpose would an empty npm repository serve unless it is a stand-alone module? npm essentially abstracts out dependencies between modules and handles it for you.
Regarding installing specific modules refer to the comment by Jan.
Maybe, it was not very clearly presented. When your point the npm client to the local registry and run using
npm install [node_module]
it is pointing to the local repository.
HI,
thank you for your answer.
For couchapp, I tried :
> node.exe node_modules/couchapp push registry/app.js http://localhost:5984/registry
but get
The “sys” module is now called “util”. It should have a similar interface.
I can replace “sys” by “util”, but in which files?
For the ‘real’ question:
>What purpose would an empty npm repository serve unless it is a stand-alone module? >npm essentially abstracts out dependencies between modules and handles it for you.
I just want to create an alternative local registry into whichI will publish my own modules, at least for the sake of learning, but I don’t want to replicate the whole npm public repository, which is the purpose of your tuto (if I’m right?)
So, for making it clearer, I want to “publish” my modules to a local couchdb instance :
- create a module “my_module” :
- npm init…
- package module : npm -registry http://hostname:5984/registry publish my_module.tgz
- Then I simply want to install my modules from this couchdb registry.
npm -registry http://hostname:5984 install my_module
I guess I should learn more about npm before asking!
regards