Testing local package without npm publish

Problem Statement:

If you are developing your own package, you would need to test it before publishing it to npm. One way to achieve it would be to require index.js file of the package in the code you are testing your package, and not by the package name. But the problem comes when the code for testing your package is already written and it is a significant effort to change all the places where you have used your package. We need a way to directly use the local version of the package, with no change in the code which is using the package.

Solution:

Go to the package directory and run the following command:

> cd examplePackage
> npm link

Next, go to the directory of your app, which has package.json having examplePackage as a dependency. Run the following command:

> npm link examplepackage

That’s it. Any change in the examplepackage will get reflected without publishing to npm.

How does it work?

The npm link command, which was run first, creates a symlink from the global directory in which the node modules are installed to the examplePackage directory. Thus this is equivalent of installing the node module globally. Use the following command to get the directory in which global node modules are installed.

> npm root -g

The second command npm link examplepackage creates a symlink inside your node_modules directory to the examplepackage directory.

Uninstall a local package

To uninstall the local package examplePackage which got installed above, use the following command in the app directory.

> npm unlink examplepackage
Kedar Chandrayan

Kedar Chandrayan

I focus on understanding the WHY of each requirement. Once this is clear, then HOW becomes easy. In my blogs too, I try to take the same approach.