NodeJS

npm install — How To Minimize Its Use Under Specific Conditions

Requirements: Are you working on a single project? Do you have to run npm install each time you create a new branch? Does it...

Written by Luci · 1 min read >

Requirements:

  1. Are you working on a single project?
  2. Do you have to run npm install each time you create a new branch?
  3. Does it take what seems a small eternity to complete?
  4. Are you fed up and want to save time?

If you answered yes to these questions, then this one is for you!


Symbolic Links To The Rescue

Instead of installing dependencies each time we create a new branch, we can keep them in a separate, central folder. We can then create symbolic links that point to node_modules in the central folder:

  1. Run npm install to generate node_modules.
  2. Create a folder in a different location, say C:\project-deps for Windows or /home/project-deps for Linux.
  3. Cut-paste node_modules into that folder.
  4. Open a command prompt (or terminal for Linux) at your branch’s folder location.
  5. Create the symbolic link.
    For Windows: mklink /D node_modules C:\project-deps\node_modules
    For Linux: ln -s /home/project-deps/node_modules node_modules

That’s it! NodeJS will treat node_modules as if it was located in your branch’s folder. From now on, you only need to checkout a branch, run the command to create the symbolic link, and you are ready to go!

Maintaining Dependencies

At some point, you or other members of your team might add new dependencies. Unfortunately, installing the new dependencies from within our branch’s folder through the symbolic link does not work as we would like.

Although node_modules is recognized by NodeJS for execution, it is not recognized for installation. It’s going to install everything as if node_modules wasn’t present.

If you ever need to install a new dependency:

  1. Copy-paste the new package.json file into the central folder.
  2. Choose to overwrite if an older version exists.
  3. Run npm install.

The first run to generate node_modules, for a relatively small project, took 250.145 seconds. Follow-up runs took significantly less time (~25 seconds), but this purely depends on the size of the dependencies installed.

Finally, we also save time when deleting merged branches — fewer files to delete means faster deletion.

Conclusion

In this article, we saw how to skip running npm install and save time by making use of symbolic links. I hope this helped speed up things a little bit for you.

Happy coding!

Written by Luci
I am a multidisciplinary designer and developer with a main focus on Digital Design and Branding, located in Cluj Napoca, Romania. Profile

Most common Node.js interview questions

Luci in NodeJS
  ·   1 min read

Create a Proxy Server For APIs

Luci in NodeJS
  ·   3 min read

Install Node.js and NPM on Window

Luci in NodeJS
  ·   3 min read
0 0 votes
Article Rating
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x