DEV Community

Bos Eriko Reyes
Bos Eriko Reyes

Posted on

How I'm avoiding using NPM in favor of Yarn

There are multiple, if not, hundreds of articles telling developers not to use NPM and instead use Yarn. I'm not going to go into details because that's not the point of this article but if you really want to know why I suggest doing a quick search. The articles that you will find will probably do more justice in explaining compared to how I will probably explain it.

Anyway, it's easy to read articles but it's really not easy to apply what you just learned to your workflow. We need to consider how many times a developer might have typed npm install in their life so no matter how hard someone tries to change there will probably be a little slip up here and there. At least that's the case for me.

A trick that saved me from that is by using aliases. My shell is zsh but even bash can have aliases. Here's how the alias looks like.

alias npm="echo 'Do you really want to use NPM instead of Yarn? \
           (Ctrl-C to abort, or press enter to continue)' && \
           read && npm"

What the snippet does is it just echoes a message asking if I really want to use NPM or if I can just use Yarn. At first, I was thinking of just adding npm as the alias of yarn but there are cases where something like that would probably be a bad idea. So this was the best I could come up with. If you want to use it as well just open up your shell's rc file. It can either be on ~/.bashrc or ~/.zshrc.

Here's how it looks like in action.

In action!

Not much but I think it's pretty cool. With this I still have the option to use NPM but I will receive a warning every time I do it. Hope this helps even one of you!

Top comments (0)