Understanding npm-link




Understanding npm-link

Sometimes you need to work on application code and a dependency at the same time. You might be the author of a dependency and don’t have good test coverage yet. The application can serve as an end-to-end test for the dependency. Maybe you need to debug an issue in your application and the problem seems to be in the dependency sources.

You could make changes in node_modules and manually copy the changes to the git repository of the dependency once you are done. But there is a much cleaner approach: npm link.

Usage

Package linking is a two-step process:

  1. Create a global symlink for a dependency with npm link. A symlink, short for symbolic link, is a shortcut that points to another directory or file on your system.
  2. Tell the application to use the global symlink with npm link some-dep.
cd ~/projects/some-dep
npm link  # Step 1.
cd ~/projects/my-app
npm link some-dep  # Step 2.

You can edit, transpile, run tests, or commit as usual in some-dep. All while my-app runs with the changes you made to some-dep. The symbolic links are local and will not be committed to git. When you are ready to share your code, publish a new version of some-dep or push to a branch that you specify in my-app’s package.json:

cd ~/projects/my-app
npm install — save some-dep@fhinkel/some-dep#experimental-branch

Debugging

If you use VSCode and want to set breakpoints in some-dep, you need to enable symlinks in the debugger for my-app. Do so by setting

“runtimeArgs”: [
  “--preserve-symlinks”
]

in launch.json.

How do you switch back to normal dependencies? When you don’t want to use the local version of some-dep anymore, delete the symlink. But careful, npm unlink is an alias for npm uninstall, it does not mirror the behavior of npm link.

cd ~/projects/my-app
npm uninstall --no-save some-dep && npm install

You can clean up the global link, though its presence won’t interfere with my-app.

cd ~/projects/some-dep
npm uninstall  # Delete global symlink

Conclusion

I used npm link while working on dependencies of the client libraries for Google Cloud Platform. All our libraries use the module @google-cloud/common. In some cases I needed to immediately see the changes in the larger libraries instead of in isolation in common.

Mastering the two-step process of npm link is a useful addition to the toolset of any Node.js developer. The process consists of running npm linkin the dependency, and npm link some-depin the application.



Author Biography.

CrowdforThink
CrowdforThink

CrowdforThink is the leading Indian media platform, known for its end-to-end coverage of the Indian startups through news, reports, technology and inspiring stories of startup founders, entrepreneurs, investors, influencers and analysis of the startup eco-system, mobile app developers and more dedicated to promote the startup ecosystem.

Join Our Newsletter.

Subscribe to CrowdforThink newsletter to get daily update directly deliver into your inbox.

CrowdforJobs is an advanced hiring platform based on artificial intelligence, enabling recruiters to hire top talent effortlessly.

CrowdforJobs

CrowdforApps brings to you the well researched list of the most successful and finest App development companies, Web software developers.

CrowdforApps

CrowdforGeeks is where lifelong learners come to learn the skills they need, to land the jobs they want, to build the lives they deserve.

CrowdforGeeks

CrowdforThink is a leading Indian media and information platform, known for its end-to-end coverage of the Indian startup ecosystem.

CrowdforThink
CFT

News & Blogs

0752431b16084e2bc68868f7908c94a1.png

Develop RESTful API using Node JS, Express JS

REST (Representational State Transfer) is web standards based architecture and uses HTTP Pro...

6f5026c52c20b8aa8e6452b8934aa46b.png

How to Build a Real-time Chat App With NodeJS, ...

In this tutorial, we’ll be building a real-time chat application with NodeJS, Express, Sock...

04dd32c0108d23bf51249aeab490ad84.png

Get a head start with trending Nodejs developer...

To build and run applications, you need different types of runtime environments that help in seve...

Top Authors

With good communication and writing skiils, Astha Sharma is a full-time content writer working wi...

Astha Sharma

Overall 3+ years of experience as a Full Stack Developer with a demonstrated history of working i...

Lokesh Gupta

Amit Agrawal Founder and COO at Cyber Infrastructure (P) Limited which is an custom software deve...

Amit Agrawal

SB Components Ltd is a specialist UK manufacturer of protective cases for single board compu...

SB Components
CFT

Our Client Says