ni

Use the right package manager

README

ni


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

ni - use the right package manager



npm i -g @antfu/ni

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. ```



nx - execute


  1. ```bash
  2. nx vitest

  3. # (not available for bun)
  4. # npx vitest
  5. # yarn dlx vitest
  6. # pnpm dlx vitest
  7. ```



nu - upgrade


  1. ```bash
  2. nu

  3. # (not available for bun)
  4. # npm upgrade
  5. # yarn upgrade (Yarn 1)
  6. # yarn up (Yarn Berry)
  7. # pnpm 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. ```



Change Directory


  1. ```bash
  2. ni -C packages/foo vite
  3. nr -C playground dev
  4. ```



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. ```



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.   $profileEntry | Out-File $profile -Append -Force
  9. }
  10. ```