What is @antfu/ni?
@antfu/ni is a command-line tool that provides a unified interface for package managers like npm, yarn, and pnpm. It simplifies the process of managing dependencies by automatically detecting the package manager used in a project and running the appropriate commands.
What are @antfu/ni's main functionalities?
Install Dependencies
The `ni` command installs dependencies in the current project. It automatically detects the package manager used (npm, yarn, or pnpm) and runs the corresponding install command.
ni
Add a Dependency
The `ni <package>` command adds a new dependency to the project. It detects the package manager and runs the appropriate command to add the specified package.
ni axios
Remove a Dependency
The `nr <package>` command removes a dependency from the project. It detects the package manager and runs the appropriate command to remove the specified package.
nr axios
Upgrade Dependencies
The `nu` command upgrades all dependencies in the project. It detects the package manager and runs the appropriate command to upgrade the dependencies.
nu
Run Scripts
The `ni run <script>` command runs a script defined in the project's package.json file. It detects the package manager and runs the appropriate command to execute the script.
ni run build
Other packages similar to @antfu/ni
npm
npm is the default package manager for Node.js. It provides a wide range of commands for managing dependencies, running scripts, and more. Unlike @antfu/ni, npm does not automatically detect other package managers.
yarn
Yarn is a fast, reliable, and secure dependency management tool. It offers similar functionalities to npm but with a focus on speed and reliability. Yarn does not provide a unified interface for other package managers like @antfu/ni.
pnpm
pnpm is a fast, disk space-efficient package manager. It uses a unique symlink-based approach to manage dependencies. Like npm and yarn, pnpm does not offer a unified interface for other package managers.
ni
npm i
in a yarn project, again? F**k!
ni - use the right package manager
npm i -g @antfu/ni
ni
npm · yarn · pnpm · bun
ni
- install
ni
ni vite
```bash
ni @types/node -D
# npm i @types/node -D
# yarn add @types/node -D
# pnpm add -D @types/node
# bun add -D @types/node
ni --frozen
ni -g eslint
nr
- run
nr dev --port=3000
nr
nr -
nx
- execute
nx vitest
nu
- upgrade
nu
nu -i
nun
- uninstall
nun webpack
nun -g eslint
nci
- clean install
nci
if the corresponding node manager is not present, this command will install it globally along the way.
na
- agent alias
na
na run foo
Change Directory
ni -C packages/foo vite
nr -C playground dev
Config
defaultAgent=npm
globalAgent=npm
export NI_CONFIG_FILE="$HOME/.config/ni/nirc"
How?
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.
Trouble shooting
Conflicts with PowerShell on Windows
PowerShell come with a built-in alias ni
for New Item
. To remove the alias in favor of this package:
PowerShell 5.x
Create or edit file C:\Windows\System32\WindowsPowerShell\v1.0\Microsoft.PowerShell_profile.ps1
, adding following line:
Remove-Item Alias:ni -Force -ErrorAction Ignore
PowerShell 7.x
Create or edit file C:\Program Files\PowerShell\7\Microsoft.PowerShell_profile.ps1
, adding following line:
Remove-Alias -Name ni -Force