npm-check-updates

repository·main·Indexed 27 days ago

https://github.com/raineorshine/npm-check-updates

A tool (ncu) that upgrades package.json dependencies to their latest versions while respecting semantic versioning policies. It is compatible with npm, yarn, pnpm, deno, and bun. Key features include interactive mode for selecting updates, target configuration (semver, minor, patch, @next), doctor mode to identify breaking changes via iterative testing, and cooldown periods to mitigate supply chain attacks.

Tokens
11.7K
Snippets
31
Records
67
Agent score
95%

What's inside npm-check-updates

  1. Understand npm-check-updates option precedence

    main

    When configuring npm-check-updates, options are merged using the following order of precedence (highest priority first):

    1. Command line options
    2. Local Config File (in the current working directory)
    3. Project Config File (located next to package.json)
    4. User Config File (located in $HOME)

    Note: Options that take no arguments can be negated by prefixing them with --no-, for example --no-peer.

  2. Check and upgrade project dependencies

    main

    Use ncu to check for latest versions of dependencies in your package.json. Note that ncu only modifies the package.json file; you must run your package manager's install command (e.g., npm install) to update the actual installed packages and package-lock.json.

    Warning: Running ncu -u will overwrite your package.json. Ensure your changes are committed to version control before proceeding.

  3. Manage monorepos with `--deep` and `--workspaces`

    main

    For monorepos, use the following patterns to scan multiple package.json files:

    • All workspaces (including root): ncu -uw
    • All package.json files under the current directory: ncu --deep -u
    • Specific glob with merged root config: ncu --packageFile 'packages/*/package.json' --mergeConfig -u
    # All workspaces, including the root.
    ncu -uw
    
    # All packages.json files under the cwd.
    ncu --deep -u
    
    # A specific glob, merging root config.
    ncu --packageFile 'packages/*/package.json' --mergeConfig -u
  4. Implement a new package manager provider

    main

    To extend npm-check-updates with support for a new package manager, implement a module that adheres to the following interface.

    • list and latest methods are required.
    • All other methods are optional and correspond to specific --target CLI values.
    • If a package is not found, methods must reject with the error string '404 Not Found'.
    {
      *list: (npmOptions: {}) => Promise<{ name: version }>,
      *latest: (pkgName: string) => Promise<String> version,
      newest: (pkgName: string) => Promise<String> version,
      greatest: (pkgName: string) => Promise<String> version,
      minor: (pkgName: string, String currentVersion) => Promise<String> version,
      patch: (pkgName: string, String currentVersion) => Promise<String> version,
    }
  5. Quick start with npm-check-updates (ncu)

    main

    Use npm-check-updates (or the shorthand ncu) to upgrade package.json dependencies. Note that ncu only modifies package.json; you must run your package manager's install command (e.g., npm install) afterward to update package-lock.json and node_modules.

    # Dry run: print upgrades without touching files.
    ncu
    
    # Write the upgrades back into package.json, then install.
    ncu -u
    npm install
    
    # Interactively pick which packages to upgrade.
    ncu -i
    
    # Only a subset of packages.
    ncu -f "react,react-dom"
    ncu "/^@types\/"
    
    # Bump only patch / minor versions.
    ncu --target patch
    ncu --target minor
    
    # Skip a package.
    ncu -x typescript,eslint
  6. Use Interactive Mode to select updates

    main

    Interactive mode allows you to manually choose which packages to upgrade from a list. You can combine this with --format group for a grouped view.

    Interactive Keys:

    • /: Select a package
    • Space: Toggle selection
    • a: Toggle all
    • Enter: Upgrade selected packages
    ncu --interactive
    # or
    ncu -i
    
    # For a grouped view
    ncu --interactive --format group
  7. Use Config Functions in .ncurc.js

    main

    For advanced configuration (using predicate functions for filter, reject, target, etc.), use a JavaScript-based configuration file.

    ESM Example:

    import { defineConfig } from 'npm-check-updates'
    
    export default defineConfig({
      upgrade: true,
      filter: name => name.startsWith('@myorg/'),
    })

    CommonJS Example:

    const { defineConfig } = require('npm-check-updates')
    
    module.exports = defineConfig({
      upgrade: true,
      filter: name => name.startsWith('@myorg/'),
    })
  8. Configure npm-check-updates via `.ncurc.*` files

    main

    You can configure ncu using .ncurc, .ncurc.json, .ncurc.yaml, .ncurc.yml, .ncurc.js, .ncurc.mjs, or .ncurc.cjs. The configuration hierarchy is: CLI flags → local .ncurc.* → project .ncurc.*$HOME/.ncurc.*.

    Since v21, the project is pure ESM. Use .ncurc.js with export default for ESM, or .ncurc.cjs for CommonJS. .ncurc.js is the only way to use predicate functions for filtering or rejecting packages.

    export default {
      filter: name => !name.startsWith('internal-'),
      reject: ['@types/*'],
      target: 'minor',
      cooldown: 7,
      doctor: false,
    }