The smart Package Manager "ni"

The smart Package Manager "ni"

Will this become the next ni-nja package manager?

We often work on multiple node-based projects in parallel and quite often there are scenarios where projects utilize distinct package managers.

And, sometimes we end up using the wrong package manager for a project.

This could lead to conflicting dependencies and lock files, resulting in unproductive time.

Luckily, there exists a solution to this issue: ni. The universal package manager.

But how does it figure out which one to use?

Now, ni expects that you're always using a .lock file.

Before it runs and starts executing commands, it will look for the packageManager field in the package.json file or the lock file in the project directory (any of the following):

  • package-lock.json

  • yarn.lock

  • pnpm-lock.yaml

  • bun.lockb

This will help to determine which package manager to use behind the scenes.

Fallback option:

if there is no .lockfile found or packageManager mentioned it will automatically use npm to run the commands. npm acts as a fallback package manager when ni is unable to decide on the package manager for a project.

; ~/.nirc

; fallback when no lock found
defaultAgent=npm # default "prompt"

; for global installs
globalAgent=npm

To install ni globally, use the following command:

npm i -g @antfu/ni

Basic commands:

Below are the basic commands that will help you understand how ni works:

  • ni ➜ Installs the dependencies for the project.

ni
# npm install
# yarn install
# pnpm install
# bun install
  • ni <PACKAGE_NAME> ➜ Installs a specific dependency for a project.
ni jsonwebtoken

# npm i jsonwebtoken
# yarn add jsonwebtoken
# pnpm add jsonwebtoken
# bun add jsonwebtoken
  • ni <PACKAGE_NAME> -D ➜ Installs a package as devDependencies .
ni nodemon -D

# npm i nodemon -D
# yarn add nodemon -D
# pnpm add -D nodemon
# bun add -d nodemon
  • nr ➜ Run a command.
nr dev --port=3000

# npm run dev -- --port=3000
# yarn run dev --port=3000
# pnpm run dev --port=3000
# bun run dev --port=3000
nr

# interactively select the script to run
# supports https://www.npmjs.com/package/npm-scripts-info convention
nr -
# Runs the last command
  • nix ➜ Execute a package.
nix vitest

# npx vitest
# yarn dlx vitest
# pnpm dlx vitest
# bunx vitest
  • nu ➜ Upgrade packages.
nu

# (not available for bun)
# npm upgrade
# yarn upgrade (Yarn 1)
# yarn up (Yarn Berry)
# pnpm update
  • nun ➜ Uninstall a package.
nun validator

# npm uninstall validator
# yarn remove validator
# pnpm remove validator
# bun remove validator
  • ni --frozen ➜ Installs comapring the package.json and package-lock.json

Read more about clean-install(ci) here.

nci

# npm ci
# yarn install --frozen-lockfile
# pnpm install --frozen-lockfile
# bun install --no-save
  • na ➜ Agent alias.
na run foo

# npm run foo
# yarn run foo
# pnpm run foo
# bun run foo
  • Run a command in a different directory

You can run a ni command in a specific directory by passing a -C flag.

ni -C <SOME_DIR>/<SUB_DIR> create-react-app
nr -C <PROJECT_DIR> dev --port=3000

I hope this has been informative and useful in expanding your technical knowledge. Thank you for reading, and please feel free to share your feedback or questions in the comments section below.

Source: