DEV Community

Cover image for Never Use the WRONG Package Manager Again!
Leonardo Montini for This is Learning

Posted on • Originally published at leonardomontini.dev

Never Use the WRONG Package Manager Again!

npm or yarn, what does this project use? Or maybe it's pnpm? Or Bun? Well, let's just use ni!

Is this a new package manager? No, no, don't worry ^^'

ni is a simple tool that will try to identify which package manager is in use in the current project, and then run the corresponding command.

How does it work?

From the docs:

ni assumes that you work with lockfiles (and you should)

Before it runs, it will detect your yarn.lock / pnpm-lock.yaml / package-lock.json / bun.lockb to know current package manager (or packageManager field in your packages.json if specified), and runs the corresponding commands.

In case you were wondering if that weird package-lock.json file that appears to change unexpectedly on npm install is needed or not, it indeed is!

Demo

I recorded a short demo to show how cool this tool is, you can watch it here:

Installation

Assuming you already have at least npm globally installed:

npm i -g @antfu/ni
Enter fullscreen mode Exit fullscreen mode

Usage

You just opened a new project and you're ready to install the dependencies. Just type ni and you're good to go! In the video demo I show it in a real use case.

Supported commands

Here's a description of all currently supported commands. In case something changes and this gets outdated, you can always check the full list on the project's README.

ni - Install dependencies

Same as npm install.

You can run it with no arguments to get the full dependencies, or with a package name to install it.

ni
ni react
Enter fullscreen mode Exit fullscreen mode

You can also set the -D flag to install as a dev dependency.

ni -D prettier
Enter fullscreen mode Exit fullscreen mode

nr - Run scripts

Same as npm run.

You can run commands in your package.json scripts.

nr start
nr build
Enter fullscreen mode Exit fullscreen mode

nlx - Download and execute a package

Same as npx.

nlx gitignore node
Enter fullscreen mode Exit fullscreen mode

Fun fact, it was originally called nx but then it was renamed to nix... and then again to nlx, to avoid confusion with other existing tools.

nu - Upgrade dependencies

Same as npm upgrade.

Upgrades all your packages to the latest version.

nu
Enter fullscreen mode Exit fullscreen mode

nun - Uninstall dependencies

Same as npm uninstall.

nun husky
Enter fullscreen mode Exit fullscreen mode

This will remove your package from package.json and uninstall it from node_modules.

nci - Clean install dependencies

Same as npm ci.

This will remove your node_modules folder and install all your dependencies again.

nci
Enter fullscreen mode Exit fullscreen mode

na - Alias for the current manager

Same as npm... when you don't know you're using npm!

na run test
Enter fullscreen mode Exit fullscreen mode

It will use the correct package manager to forward the command.

Conclusion

As an Open Source contributor it happens quite often to switch between projects that use different package managers. This tool is a great way to avoid having to remember which one is used where.

Even if most of the time npm is the right one, just typing ni or nr is faster anyway.

I hope you'll find this useful too!


Thanks for reading this article, I hope you found it interesting!

I recently launched my Discord server to talk about Open Source and Web Development, feel free to join: https://discord.gg/bqwyEa6We6

Do you like my content? You might consider subscribing to my YouTube channel! It means a lot to me ❀️
You can find it here:
YouTube

Feel free to follow me to get notified when new articles are out ;)

Top comments (5)

Collapse
 
77pintu profile image
77pintu

Thanks for the great post

Collapse
 
balastrong profile image
Leonardo Montini

Glad you like it!

Collapse
 
jmfayard profile image
Jean-Michel πŸ•΅πŸ»β€β™‚οΈ Fayard

Next step: support package managers for Python/Java/Rust/...
Just kidding :)

Collapse
 
balastrong profile image
Leonardo Montini

I mean, if there's not one... time to create a new repo on GitHub :D

Collapse
 
westtrade profile image
Gennadiy Popov

Why not use Volta? This tool are pin node and package manager vesions and allow to transparent use needed version.

Also as I think if somone combine this tools it would be awesome.