ni

repository·main·Indexed 27 days ago

https://github.com/antfu-collective/ni

A CLI tool that automatically detects and uses the correct package manager (npm, yarn, pnpm, bun, or deno) for a project based on its lockfiles. It provides a unified interface for common tasks: ni for installing/adding packages, nr for running scripts, nlx for executing packages, nup for upgrading dependencies, nun for uninstalling, nci for clean installs, and nd for deduplication.

Tokens
3.7K
Snippets
14
Records
34
Agent score
89%

What's inside @antfu/ni

  1. Install dependencies with `ni`

    main

    Use ni to install packages. It automatically detects whether to use npm, yarn, pnpm, bun, or deno based on your project's lockfile.

    • ni: Runs the standard install command (e.g., npm install).
    • ni <package>: Adds a package (e.g., ni vite).
    • ni <package> -D: Adds a package to devDependencies (e.g., ni @types/node -D).
    • ni -P: Installs production dependencies only.
    • ni --frozen: Performs a frozen/clean install (e.g., npm ci).
    • ni -g <package>: Installs a package globally.
    • ni -i: Interactively search and select a package to install.
    ni vite
    ni @types/node -D
    ni -P
    ni --frozen
    ni -i
  2. Uninstall dependencies with `nun`

    main

    Use nun to remove packages.

    • nun <package>: Removes a specific package.
    • nun: Interactively multi-select dependencies to remove.
    • nun -g <package>: Removes a package globally.
    nun webpack
    nun
    nun -g silent
  3. Run scripts with `nr`

    main

    Use nr to execute scripts defined in your package.json using the appropriate package manager.

    • nr <script>: Runs a specific script (e.g., nr dev --port=3000).
    • nr: Interactively select a script to run.
    • nr -: Reruns the last command.
    • nr -p: Interactively select a package and then a script to run.
  4. Use pnpm catalogs with `ni`

    main

    When working in a pnpm workspace with catalogs configured, ni automatically enters catalog mode.

    • It writes catalog: references into package.json instead of pinned versions.
    • If a package is not in a catalog, ni will prompt you to select a catalog or skip.
    • Flags like -D are respected (e.g., ni typescript -D writes to devDependencies).
    • Use -w or --workspace to target the workspace root package.json (e.g., ni react -w).

    To disable catalog mode, set catalog=false in ~/.nirc or use the NI_CATALOG=false environment variable.

    ni react -w
  5. Configure `ni` via `.nirc` or Environment Variables

    main

    You can customize ni behavior using a .nirc configuration file or environment variables.

    Configuration Keys

    • defaultAgent: Fallback agent when no lockfile is found (e.g., npm).
    • globalAgent: Agent used for global installs.
    • runAgent: Agent used for running scripts (e.g., node for Node.js 22+).
    • useSfw: Prefix commands with sfw.
    • catalog: Enable/disable catalog mode (default true).

    Environment Variables

    • NI_CONFIG_FILE: Path to custom configuration file.
    • NI_DEFAULT_AGENT: Sets the default agent.
    • NI_GLOBAL_AGENT: Sets the global agent.
    • NI_USE_SFW: Enables/disables sfw prefix.
    • NI_CATALOG: Enables/disables catalog mode.
    • NI_AUTO_INSTALL: Set to true to automatically install the required package manager globally if missing.
    ; ~/.nirc
    defaultAgent=npm
    globalAgent=npm
    runAgent=node
    useSfw=true
    catalog=true
  6. Use the ni CLI for package management

    main

    ni is a CLI tool that simplifies package management by automatically detecting the package manager used in a project (e.g., npm, yarn, pnpm, bun, or deno) and running the appropriate install or add command. Instead of remembering specific flags for different managers, you can use universal ni commands.

    Supported Package Managers

    • npm
    • yarn
    • pnpm
    • bun
    • deno

    Common Usage Patterns

    • Install dependencies: Use ni to run the project's install command.
    • Add a package: Use ni <package-name> to add a dependency using the detected package manager.
  7. Resolve PowerShell `ni` alias conflict

    main

    In PowerShell, ni is a built-in alias for New-Item. To use the ni package instead, you must remove the alias.

    To remove it for the current session:

    'Remove-Item Alias:ni -Force -ErrorAction Ignore'

    To persist this, add the command to your PowerShell profile script.