setup-node

repository·main·Indexed 26 days ago

https://github.com/actions/setup-node

A GitHub Action that automates the installation and configuration of Node.js environments. It supports Node.js version management via SemVer or version files, dependency caching for npm, yarn, and pnpm, registry authentication, and target architecture selection. Version 7.0.0 includes features for automatic package manager caching and support for V8 Canary builds.

Tokens
7.2K
Snippets
19
Records
39
Agent score
82%

What's inside setup-node

  1. Use private packages with Yarn Berry (v2+)

    main

    Yarn Berry (v2+) ignores .npmrc and .yarnrc files created by the action. To use private registries, you must manually configure .yarnrc.yml using yarn config set commands before running your install step.

    steps:
    - uses: actions/checkout@v7
    - uses: actions/setup-node@v7
      with:
        node-version: '24.x'
        package-manager-cache: false
    - name: Setup .yarnrc.yml
      run: |
        yarn config set npmScopes.my-org.npmRegistryServer "https://npm.pkg.github.com"
        yarn config set npmScopes.my-org.npmAlwaysAuth true
        yarn config set npmScopes.my-org.npmAuthToken $NPM_AUTH_TOKEN
      env:
        NPM_AUTH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
    - name: Install dependencies
      run: yarn install --immutable
  2. Review pull requests

    main

    Reviewing pull requests helps maintain project quality.

    How to review:

    1. Navigate to the pull requests page.
    2. Ensure you are familiar with the code or documentation being updated.
    3. Use GitHub's review functionality to ask clarifying questions, point out errors, or suggest alternatives.
    4. Submit your review as a comment, an approval, or a request for changes. Consider whether 'nitpicks' are actual blockers to merging.
  3. Provide support on issues

    main

    You can contribute by helping users in the issue tracker.

    Guidelines:

    • Respond to issues you are familiar enough to answer accurately.
    • Link to past issues with accepted answers when providing solutions.
    • Be kind and patient with users.
    • Once a discussion is resolved, ask the original filer or a maintainer to close the issue.
  4. Install and use setup-node

    main

    Use actions/setup-node@v7 to download and cache Node.js distributions, add them to the PATH, cache npm/yarn/pnpm dependencies, register problem matchers, and configure authentication for GPR or npm.

    It is recommended to always specify a node-version rather than relying on the system default.

    steps:
    - uses: actions/checkout@v7
    - uses: actions/setup-node@v7
      with:
        node-version: 24
        package-manager-cache: false # Disable automatic npm caching if not required
    - run: npm ci
    - run: npm test
  5. Publish to npm using Trusted Publisher (OIDC)

    main

    npm Trusted Publishers allow publishing via OpenID Connect (OIDC) instead of static tokens.

    Requirements

    • npm ≥ 11.5.1 (required)
    • Node.js 24 or newer (recommended)
    • A Trusted Publisher must be configured in npm for your specific GitHub repository and workflow.

    Implementation

    1. Set permissions to include id-token: write.
    2. Set package-manager-cache: false to prevent credential exposure via cache poisoning.

    Note: If the Trusted Publisher configuration does not match the workflow run identity exactly, publishing will fail with E404 Not Found.

        permissions:
          contents: read
          id-token: write
    
        steps:
          - uses: actions/checkout@v7
    
          - uses: actions/setup-node@v7
            with:
              node-version: '24'
              registry-url: 'https://registry.npmjs.org'
              package-manager-cache: false
    
          - run: npm ci
          - run: npm run build --if-present
          - run: npm publish
  6. Configure package manager lockfiles for CI

    main

    To ensure consistent dependency installation in CI environments, follow these conventions for your package manager:

    • NPM: Commit package-lock.json and use npm ci instead of npm install.
    • Yarn: Commit yarn.lock and use yarn install --immutable.
    • PNPM: Commit pnpm-lock.yaml and use pnpm install --frozen-lockfile.
  7. Set recommended permissions for `setup-node`

    main

    To ensure the action can properly check out code and install dependencies, it is recommended to set the contents: read permission in your workflow.

    permissions:
      contents: read # access to check out code and install dependencies
  8. Cache npm, yarn, or pnpm dependencies

    main

    The action provides built-in caching for npm, yarn, and pnpm (v6.10+) using actions/cache under the hood. It caches global package data but does not cache node_modules.

    • Automatic npm caching: Enabled by default if package-manager-cache is true and your package.json has the packageManager or devEngines.packageManager field set to npm.
    • Manual caching: Use the cache input to specify the package manager (npm, yarn, or pnpm).
    • Custom dependency paths: Use cache-dependency-path to point to specific lockfiles (e.g., in monorepos).
    # Caching npm dependencies
    - uses: actions/setup-node@v7
      with:
        node-version: 24
        cache: 'npm'
    
    # Caching in a monorepo
    - uses: actions/setup-node@v7
      with:
        node-version: 24
        cache: 'npm'
        cache-dependency-path: subdir/package-lock.json
  9. Enable dependency caching for npm or yarn

    main

    You can speed up dependency installation by enabling built-in caching for npm or yarn within the actions/setup-node action. This eliminates the need for manual actions/cache configuration for standard use cases.

    Requirements & Behavior:

    • The action expects package-lock.json (for npm) or yarn.lock (for yarn) to be located in the repository root. If neither file is found, the action will throw an error.
    • The cache key is automatically generated using the hash of the lock file and the runner OS (e.g., ${{ runner.os }}-npm-${{ hashFiles('package-lock.json') }}).
    • Caching is disabled by default.

    Supported cache input values:

    • npm: Enable caching for npm dependencies.
    • yarn: Enable caching for yarn dependencies.
    • '' (empty string): Disable caching (default).
    ### Npm package manager
    ```yml
    steps:
    - uses: actions/checkout@v2
    - uses: actions/setup-node@v2
      with:
        node-version: '14'
        cache: npm

    Yarn package manager

    steps:
    - uses: actions/checkout@v2
    - uses: actions/setup-node@v2
      with:
        node-version: '14'
        cache: yarn
  10. Contribute documentation to setup-node

    main

    Documentation contributions are welcome and can range from fixing typos to rewording sentences for clarity.

    To contribute:

    1. Check that no one else has already created a PR for the same changes.
    2. Use a feature branch.
    3. Ensure changes are formatted consistently with existing documentation.
    4. Run a spellchecker on your changes.
    5. If the PR is connected to an open issue, include a Related issue: link.
    6. Submit via Pull Request.
  11. Cache dependencies with setup-node

    main

    The action can automatically cache global package data to speed up workflows. This follows actions/cache guidelines and caches the global cache on the machine rather than node_modules, allowing reuse across different Node.js versions.

    Yarn Caching

    Supports Yarn Classic (v1) and Yarn Berry (v2, v3, v4+).

    pnpm Caching

    Requires pnpm version >= 6.10.0.

    Customizing Cache Paths

    You can use cache-dependency-path to specify which files trigger a cache update:

    • Wildcard patterns: e.g., **/package-lock.json.
    • List of paths: A multi-line string of specific file paths.
    # Yarn example
    steps:
    - uses: actions/checkout@v7
    - uses: actions/setup-node@v7
      with:
        node-version: '24'
        cache: 'yarn'
    - run: yarn install --frozen-lockfile
    
    # pnpm example (requires pnpm >= 6.10.0)
    steps:
    - uses: actions/checkout@v7
    - uses: pnpm/action-setup@v6
      with:
        version: 10
    - uses: actions/setup-node@v7
      with:
        node-version: '24'
        cache: 'pnpm'
    - run: pnpm install
    
    # Custom dependency paths (npm example)
    steps:
    - uses: actions/checkout@v7
    - uses: actions/setup-node@v7
      with:
        node-version: '24'
        cache: 'npm'
        cache-dependency-path: '**/package-lock.json'
    - run: npm ci
  12. Configure dependency caching for monorepos using cache-dependency-path

    main

    When using a monorepo or a repository where the dependency lock file (package-lock.json or yarn.lock) is not located in the repository root, use the cache-dependency-path input.

    This input accepts a path (relative to the repository root) to the dependency lock file. If you provide a path containing wildcards, the action will search for all matching files and use a hash of those files as part of the cache key, similar to how ${{ hashFiles('...') }} works in GitHub Actions workflows.

    Supported use cases:

    • Specifying a specific lock file in a subdirectory.
    • Using file patterns (e.g., **/package-lock.json) to match multiple files.
    • Specifying custom dependency files like src/npm-shrinkwrap.json.
    • Overriding the default resolution priority between yarn.lock and package-lock.json.
    steps:
    - uses: actions/checkout@v2
    - uses: actions/setup-node@v2
      with:
        node-version: 14
        cache: npm
        cache-dependency-path: 'sub-project/package-lock.json'