banner image 1 banner image 2

Is there any way to develop and test the reusable packages at the same time without re-installing…

December 23, 2022
3 mins
command
blog-img 1
Vinoth S
Author

Yes, One of the NPM commands will perform that trick. -Npm Link

By Vinoth S — “An Enthusiast Coder thrives for learning”


Problem statement

As a developer, I used to think of a way to test the reusable component in my main application before publishing the changes to the remote server.

By default, I will complete the coding for project library “B” and push it to the remote server and then I will use project “B” in the dependencies of Project “A”.

And then the npm install helps here..Finally !! Here comes the real-time testing phase

Are we need such a long process? The wait is over by using the “npm Link” command.

Keywords:

  1. npm link– Used to create a symlink for your reusable resources and the same can be mapped with your main project as well.

2. npm unlink– Once you’re done with real-time testing, then you can break the symlink by using the npm unlink command.

Advantages:

  • Realtime testing can be done at the time of development itself. Hence the separate time for testing can be reduced.
  • Since we have established symlink, No need to re-installing packages after every change in the reusable library.

Usecase:

For example, I have 20+ microservices in the global project where we have repeated code related to HTTP calls, Messaging queues implementation, caching, database / elastic search/ AWS connection, etc in each microservices.

To optimize this, we will create a “reusable-lib”, move the set of repeated codes to a separate repository and publish it as a separate module. Let’s create one shared library named “reusable-lib” which contains the different basic functionalities required for our main application and the same can be used across different microservices in our project.

Real time Example:

Now let’s start with real-time implementation where we have one main project named “core-app” and one shared project “reusable-lib”.

After creating the npm link for “reusable lib”, now we can use the same in other projects.

Now the symlink has been established between “reusable-lib” and “core-app”.

Develop and test the reusable packages at the same time

We can create a new file “index.js” in the “reusable-lib” and without using the npm install command in “core-app”, the same will be available within the “node_modules” folder of our “core-app”.

Now the index.js file within the “reusable-lib” and “core-app” will have a symlink and the changes will reflect in real-time.

Once we have done with our real-time testing, then it’s time to remove the symlink from “reusable-lib” using the npm unlink command. Then, “reusable-lib” will be no longer accessed in real-time from other projects.

Hope this information helps !! It will reduce some of our developer’s time in the testing phase.

Thank you for reading the article!! I appreciate your feedback :)


Meet The Team!

Author

Vinoth S

Edited By

Seema Jain


We at CaratLane are solving some of the most intriguing challenges to make our mark in the relatively uncharted omnichannel jewellery industry. If you are interested in tackling such obstacles, feel free to drop your updated resume/CV to careers@caratlane.com
blog-img 2

Discussions

blog-img 3
5 mins
May 17, 2023
Sharing Data Between Controllers: Best Practices S...

This article will help you to understand the diffe

By Naveen C

blog-img 3
5 mins
March 21, 2023
Understanding Auto Layout and Constraints in Swift...

This article gives you an easy way of understandin

By Ramasamy P