DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
The Latest "Software Integration: The Intersection of APIs, Microservices, and Cloud-Based Systems" Trend Report
Get the report
  1. DZone
  2. Software Design and Architecture
  3. Security
  4. How to Use 'npm link' to Develop Sharable Components

How to Use 'npm link' to Develop Sharable Components

Read on to learn how to use node's npm link command to connect your parent application to a module you have locally on your machine

Toby Retallick user avatar by
Toby Retallick
·
May. 08, 17 · Tutorial
Like (0)
Save
Tweet
Share
9.17K Views

Join the DZone community and get the full member experience.

Join For Free

If you are working on a node project there may come a point where your app has a dependency on another custom module you have created.

For example, at one of our clients, my colleagues and I are responsible for a shared component called the Search Component. Like many of the 'shared' libraries used in this particular project, the Search Component is published to, and accessed from, a private Artifactory repository. It can be inserted anywhere in the application (usually the header), allowing the user to easily search the site's content and instantly see a results summary.

Above: The Search Component is a custom node module that can be inserted anywhere in the parent application

Although automated testing certainly has its place, when it comes to working with shared components, particularly those that are user-facing, manual testing can provide some benefits. When you are at the early stages of feature implementation or spiking, sometimes it makes more sense to manually test the effects of your changes at the user interface level and iterate accordingly. Once you stabilize, then robust tests can (and should) be put in place.

This was particularly true for me when it came to working with the Search Component. Once the app was started, modifying the Search Component in place thereafter would not result in any observable changes.

Luckily, the node package manager (npm) has a way to make development with dependencies a lot easier via the npm linkcommand.

'npm Link' in a Nutshell

The npm documentation defines npm link as a 'means to symlink a package folder.' Put simply, it's a means to connect your parent application to a module you have locally on your machine. When you run the application, any changes you make to the dependency will be reflected in the application.

How to 'npm ink'

  1. In the terminal, navigate to the folder of the dependency you want to modify and run the command npm link. This makes the component globally available to the rest of your application.
  2. Navigate to the folder of the parent application (i.e., the one that depends on the module you want to modify). In the root run the command npm link [name of module you want to modify]
  3. Next, run both the parent application and the module dependency, then start making changes to the dependency to your heart's content. In addition, using a library that watches for changes in your code and rebuilds your app such as Nodemon will be essential in order to save you from restarting the application all the time.
  4. Finally, once you have all the changes in place (plus tests!), navigate to the folder of the parent application and unlink the dependency using npm unlink [name of module you modified]. This will remove the local dependency from the parent application. Similarly, navigate to the folder of the dependency you modified and run npm unlink to no longer make the module globally available. Publish your (modified) dependency, then access the latest version by doing a standard npm install.

One of the great things about the node ecosystem is that it encourages single responsibility by making it easy to create and deploy tightly defined modules that can be shared across your wider application. This decoupling, however, makes the live modification of your dependencies a little trickier, but as we have seen, its a case of using the tools provided.

Just npm-link all-the-things and hopefully this will make life a little easier for you.

application Links Dependency Npm (software)

Published at DZone with permission of Toby Retallick, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Microservices Testing
  • Application Architecture Design Principles
  • gRPC on the Client Side
  • Testing Level Dynamics: Achieving Confidence From Testing

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: