Streamlining Your Dependencies with ni
A Beginner’s Tutorial to the Package Manager of the Future
I thus cloned an old project but was unable to determine whether it was using npm or yarn as a package management. Or might it be pnpm? Or Bun? I guess I’ll just use ni!
Is this a brand-new packager? No, no, don’t be alarmed.
The straightforward utility ni will attempt to determine which package management is being used in the current project before executing the appropriate command.
How does it function?
ni assumes you use lockfiles, which you should.
It will identify your current package manager (or the packageManager field in your packages.json, if given), yarn.lock / pnpm-lock.yaml / package-lock.json / bun.lockb performs the appropriate tasks.
In case you were unsure whether or not you needed that strange package-lock.json file that seems to change suddenly upon npm install, you do!
Installation
I already have at least npm globally installed:
npm i -g @antfu/ni
Usage
You have a new project open and all set to install the requirements. You can start by typing ni.
Guaranteed commands
Here is a list of all the commands that are currently available. You may always check the whole list in the project’s README if something changes and this information becomes stale.
ni — to install dependenciesSame as npm install.
Let’s install react.
ni react
You can also use the -D flag to install as a dev dependency.
ni -D prettier
You can run commands in your package.json scripts.
nr startnr build
nlx - Download and execute a package
Same as npx.
nlx gitignore node
nu - Upgrade dependencies
Same as npm upgrade.
Upgrades all your packages to the latest version.
nu
nun - Uninstall dependencies
Same as npm uninstall.
nun husky
This will remove your package from package.json and uninstall it from node_modules.
nci - Clean install dependencies
Same as npm ci.
This will purge your node_modules folder and install your dependencies again.
nci
na - Alias for the current manager
Same as npm
na run test
It will use the correct package manager to forward the command.
Conclusion
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!
More content at PlainEnglish.io.
Sign up for our free weekly newsletter. Follow us on Twitter, LinkedIn, YouTube, and Discord.
Streamlining Your Dependencies with ni: A Beginner’s Tutorial to the Package Manager of the Future. was originally published in JavaScript in Plain English on Medium, where people are continuing the conversation by highlighting and responding to this story.