When working on multiple related JavaScript projects, you may need to test changes in a local dependency without publishing it to npm. npm link provides a way to symlink a local package, allowing real-time updates without reinstalling the package manually. However, improper usage can lead to issues like dependency mismatches or unresolved modules.
npm link worksnpm link creates a symbolic link between a globally registered local package and a project that depends on it.
npm link
This registers the package globally on your system.
npm link <package-name>
This links the globally registered package into your project’s node_modules.
If you're developing my-local-package and want to use it in another project:
cd ~/projects/my-local-packagenpm linkcd ~/projects/my-appnpm link my-local-package
Now, my-app will use the local version of my-local-package instead of fetching it from npm.
npm install
in both the linked package and the main project.
tsc --watch) or adding "preserveSymlinks": true in tsconfig.json can help.npm linknpm install <path>Instead of linking, you can install a local package directly:
npm install ../my-local-package
✅ Figure: Good example - Simpler alternative that avoids symlink-related issues
npm packAnother option is to create a tarball of the package and install it manually:
cd my-local-packagenpm packnpm install ../my-local-package/my-local-package-1.0.0.tgz
✅ Figure: Good example - Simulates a real npm package installation
npm link mainly for development, not for production environmentsnpm install <path> or npm pack if npm link causes issuesBy understanding and properly using npm link, you can streamline local package development while avoiding common pitfalls.