The solution for a working npm/yarn link

Mattia Franchetto
2 min readMar 5, 2018

--

Every NodeJS developer comes to a point where a local package needs to be linked to an application to run some tests before publishing a new version on the registry. If you focus enough, you can hear developers from all around the world screaming after the most dangerous command has been run.

The problem is the way linking is done: npm/yarn use symbolic links to connect packages, duplicating dependencies that cannot be deduped since dedupe does not work for symlinks. This behaviour leads to an infinite number of problems, for example when only a version of a given package needs to be loaded for working properly, or when working with Typescript, where types packages loaded more than once cause tsc to fails in horrible and scary ways. Luckily for us a solution has been found!

You cannot be serious! I tried everything and nothing works!

It was a cloudy and dull afternoon when I ran into this Github issue.

The proposed change is really simple: instead of using symbolic links, just mimic what an actual publish might do. When you have made some changes to a module, pack all the files that npm will publish on the registry and instead of going public, put them on a local registry (a folder) and point the main package to it. Sounds complicated, right? Again, we are lucky because someone helped us with a simple CLI tool: say hello to yalc!

A simple scenario

You have project A that depends on module B and B needs to be modified and tested locally.

cd moduleB
// do some changes
yalc publish // this will make the module available for other projects
cd moduleA
yalc add [module_B_name]

Building your project will result in a new version with the modified module B, same result as you would have run npm link, the only differences is that you won’t run into any troubles.

N.B. If the new version of B requires additional dependencies just remember to run npm/yarn install on your project.

Bonus tip

In a normal working scenario many changes are made to module B before publishing it, so running yalc publish/add every time is a waste of precious time, but yalc still has us covered!

cd moduleB
// Do some changes
yalc push // the new version is published and pushed to all dependent projects

This is everything you need to get started with yalc, super simple but very effective. Please let me know if yalc worked for you too!

--

--