ni

Use the right package manager

README

ni


npm i in a yarn project, again? F\\k!

ni - use the right package manager

  1. ```
  2. npm i -g @antfu/ni
  3. ```

npm · yarn · pnpm · bun


ni - install


  1. ```bash
  2. ni

  3. # npm install
  4. # yarn install
  5. # pnpm install
  6. # bun install
  7. ```

  1. ```bash
  2. ni vite

  3. # npm i vite
  4. # yarn add vite
  5. # pnpm add vite
  6. # bun add vite
  7. ```

  1. ```bash
  2. ni @types/node -D

  3. # npm i @types/node -D
  4. # yarn add @types/node -D
  5. # pnpm add -D @types/node
  6. # bun add -d @types/node
  7. ```

  1. ```bash
  2. ni --frozen

  3. # npm ci
  4. # yarn install --frozen-lockfile (Yarn 1)
  5. # yarn install --immutable (Yarn Berry)
  6. # pnpm install --frozen-lockfile
  7. # bun install --no-save
  8. ```

  1. ```bash
  2. ni -g eslint

  3. # npm i -g eslint
  4. # yarn global add eslint (Yarn 1)
  5. # pnpm add -g eslint
  6. # bun add -g eslint

  7. # this uses default agent, regardless your current working directory
  8. ```



nr - run


  1. ```bash
  2. nr dev --port=3000

  3. # npm run dev -- --port=3000
  4. # yarn run dev --port=3000
  5. # pnpm run dev --port=3000
  6. # bun run dev --port=3000
  7. ```

  1. ```bash
  2. nr

  3. # interactively select the script to run
  4. # supports https://www.npmjs.com/package/npm-scripts-info convention
  5. ```

  1. ```bash
  2. nr -

  3. # rerun the last command
  4. ```



nlx - download & execute


  1. ```bash
  2. nlx vitest

  3. # npx vitest
  4. # yarn dlx vitest
  5. # pnpm dlx vitest
  6. # bunx vitest
  7. ```



nu - upgrade


  1. ```bash
  2. nu

  3. # npm upgrade
  4. # yarn upgrade (Yarn 1)
  5. # yarn up (Yarn Berry)
  6. # pnpm update
  7. # bun update
  8. ```

  1. ```bash
  2. nu -i

  3. # (not available for npm & bun)
  4. # yarn upgrade-interactive (Yarn 1)
  5. # yarn up -i (Yarn Berry)
  6. # pnpm update -i
  7. ```



nun - uninstall


  1. ```bash
  2. nun webpack

  3. # npm uninstall webpack
  4. # yarn remove webpack
  5. # pnpm remove webpack
  6. # bun remove webpack
  7. ```

  1. ```bash
  2. nun -g silent

  3. # npm uninstall -g silent
  4. # yarn global remove silent
  5. # pnpm remove -g silent
  6. # bun remove -g silent
  7. ```

nci - clean install


  1. ```bash
  2. nci

  3. # npm ci
  4. # yarn install --frozen-lockfile
  5. # pnpm install --frozen-lockfile
  6. # bun install --no-save
  7. ```

if the corresponding node manager is not present, this command will install it globally along the way.



na - agent alias


  1. ```bash
  2. na

  3. # npm
  4. # yarn
  5. # pnpm
  6. # bun
  7. ```

  1. ```bash
  2. na run foo

  3. # npm run foo
  4. # yarn run foo
  5. # pnpm run foo
  6. # bun run foo
  7. ```

Global Flags


  1. ```bash
  2. # ?               | Print the command execution depends on the agent
  3. ni vite ?

  4. # -C              | Change directory before running the command
  5. ni -C packages/foo vite
  6. nr -C playground dev

  7. # -v, --version   | Show version number
  8. ni -v

  9. # -h, --help      | Show help
  10. ni -h
  11. ```



Config


  1. ```ini
  2. ; ~/.nirc

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

  5. ; for global installs
  6. globalAgent=npm
  7. ```

  1. ```bash
  2. # ~/.bashrc

  3. # custom configuration file path
  4. export NI_CONFIG_FILE="$HOME/.config/ni/nirc"
  5. ```

  1. ```ps
  2. # for Windows

  3. # custom configuration file path in PowerShell accessible within the `$profile` path
  4. $Env:NI_CONFIG_FILE = 'C:\to\your\config\location'
  5. ```



Integrations


asdf


You can also install ni via the 3rd-party asdf-plugin maintained by CanRau

  1. ```bash
  2. # first add the plugin
  3. asdf plugin add ni https://github.com/CanRau/asdf-ni.git

  4. # then install the latest version
  5. asdf install ni latest

  6. # and make it globally available
  7. asdf global ni latest
  8. ```

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


PowerShell comes with a built-in alias ni for the New-Item cmdlet. To remove the alias in your current PowerShell session in favor of this package, use the following command:

  1. ```PowerShell
  2. 'Remove-Item Alias:ni -Force -ErrorAction Ignore'
  3. ```

If you want to persist the changes, you can add them to your PowerShell profile. The profile path is accessible within the $profile variable. The ps1 profile file can normally be found at

- PowerShell 5 (Windows PowerShell): C:\Users\USERNAME\Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1
- PowerShell 7: C:\Users\USERNAME\Documents\PowerShell\Microsoft.PowerShell_profile.ps1
- VSCode: C:\Users\USERNAME\Documents\PowerShell\Microsoft.VSCode_profile.ps1

You can use the following script to remove the alias at shell start by adding the above command to your profile:

  1. ```PowerShell
  2. if (-not (Test-Path $profile)) {
  3.   New-Item -ItemType File -Path (Split-Path $profile) -Force -Name (Split-Path $profile -Leaf)
  4. }

  5. $profileEntry = 'Remove-Item Alias:ni -Force -ErrorAction Ignore'
  6. $profileContent = Get-Content $profile
  7. if ($profileContent -notcontains $profileEntry) {
  8.   ("`n" + $profileEntry) | Out-File $profile -Append -Force -Encoding UTF8
  9. }
  10. ```

nx and nix is no longer available


We renamed nx/nix to nlx to avoid conflicts with the other existing tools - nx and nix. You can always alias them back on your shell configuration file (.zshrc, .bashrc, etc).

  1. ```bash
  2. alias nx="nlx"
  3. # or
  4. alias nix="nlx"
  5. ```