Taking Baby Steps with Node.js – Linking Local Packages with npm

< The list of previous installments can be found here. >

I just wanted to share a very neat feature of npm that makes life quite a lot easier when developing libraries for Node.js. Suppose that we?re building our very own ORM library for Node.js (we all build an ORM at some point in our career, right?). In order to have our dog food and eat it too, we want to start using our ORM library in an application as soon as possible. As with all other modules, we want to use npm in order to add it as a dependency of our application. But we also don?t want to publicly publish our half-baked ORM package to the npm repository either. This is where creating an npm link saves the day.

In the local folder of our library we have to create a link by issuing the following command:

~/kick-ass-orm$  npm link

After that we can add this package to the node-modules folder of our application by executing the following command:

~/facebook-killer$ npm link kick-ass-orm

There you have it. Notice that a new folder with the name of the package is added to the node_modules folder of the application. This is simply a symlink to the package folder of our ORM library. This means that when we make changes to this library, those changes are also exposed to our application as well.

As soon as we feel comfortable enough about our ORM library, we can make it publicly available in the npm repository. We can then simply remove the link from our application by issuing the following command:

~/facebook-killer$ npm unlink kick-ass-orm

At this point we add our freshly published package to the package.json file of our application. We can also remove the link from our local package folder by executing the following command:

~/kick-ass-orm$ npm unlink

This particular feature has been very useful to me over the last couple of weeks.

npm ? is there anything it can?t do?

Until next time.

Proudly powered by WordPress | Theme: Code Blog by Crimson Themes.