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 linkNext, go to the directory of your app, which has package.json having examplePackage as a dependency. Run the following command:
> npm link examplepackageThat’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 -gThe 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