DEV Community

Cover image for Migrating from NPM to Yarn
Sunny Golovine
Sunny Golovine

Posted on

22 1

Migrating from NPM to Yarn

Say you have a project that uses NPM for dependency management. How would you convert that project to use Yarn?

For the longest time the way I did it was just deleting the package-lock.json and running yarn to generate a new lockfile. While this works well on small projects, dependency pinning is there for a reason and trying to do this on a larger project may land you in hot water. So how do you do it safely? It's simple:


yarn import

Enter fullscreen mode Exit fullscreen mode

That's it! When you run yarn import, yarn will read your package-lock.json and generate a yarn.lock file based on it. It's a much safer way to migrate from one package manager to the other.

If you enjoyed this post check out some of my other writing

Hostinger image

Get n8n VPS hosting 3x cheaper than a cloud solution

Get fast, easy, secure n8n VPS hosting from $4.99/mo at Hostinger. Automate any workflow using a pre-installed n8n application and no-code customization.

Start now

Top comments (7)

Collapse
 
frontendengineer profile image
Let's Code

npm is catching up nicely or has caught up with yarn already.

What is the advantage or use case of converting to yarn now?

Collapse
 
andreidascalu profile image
Andrei Dascalu

It has in terms of features. I have a proiect (fairly large scale) where migrating from npm 7.4 to yarn cut the build time in almost half.
I'm not an expert in the intricacies of either but given the pretty "out of the box" approach in both instances, I'll take the change (even if there was an optimization that could be done to configure npm better)

Collapse
 
lyrod profile image
Lyrod

I would like to know how many time you save with "pnpm" !

Thread Thread
 
andreidascalu profile image
Andrei Dascalu

By the way, I found this: reddit.com/r/learnjavascript/comme... - if you look at the last comment, someone did a benchmark of Yarn2 vs pnpm about 4 months ago, so it should be fairly relevant.

Collapse
 
lyrod profile image
Lyrod

pnpm is for me, the faster package manager. I use. I use it for all my open source project

Collapse
 
frontendengineer profile image
Let's Code

i see. that is significant change. i would see myself migrating as well. thanks.

Collapse
 
jentanbernardus profile image
Jentan Bernardus

That's a useful tip, thnx for sharing.

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

Instrument, monitor, fix: a hands-on debugging session

Join Lazar for a hands-on session where you’ll build it, break it, debug it, and fix it. You’ll set up Sentry, track errors, use Session Replay and Tracing, and leverage some good ol’ AI to find and fix issues fast.

Tune in to the full event

DEV is partnering to bring live events to the community. Join us or dismiss this billboard if you're not interested. ❤️