Requirements:
- Are you working on a single project?
- Do you have to run
npm install
each time you create a new branch? - Does it take what seems a small eternity to complete?
- 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:
- Run
npm install
to generatenode_modules
. - Create a folder in a different location, say
C:\project-deps
for Windows or/home/project-deps
for Linux. - Cut-paste
node_modules
into that folder. - Open a command prompt (or terminal for Linux) at your branch’s folder location.
- 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:
- Copy-paste the new
package.json
file into the central folder. - Choose to overwrite if an older version exists.
- 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!