Skip to content

Enable caching by default if packageManager field is defined in package.json #686

Description

@trivikr

Description:
The action allows caching global packages data as per documentation in https://github.com/actions/setup-node#caching-global-packages-data

It's an amazing feature and reduces action execution time, but it has to be enabled in configuration.
Caching is not enabled in default setup. Users may not discover caching feature by default, and miss on the faster executions times enabled by caching.

The feature request is to enable caching by default if packageManager field is defined.
This can be enabled in a major version bump, say v4, to not affect existing users.

Justification:
Just want all users to get benefit of faster executions times allowed by caching without explicitly opting in, if they have set up packageManager field in their package.json

They can always explicitly opt-out by setting cache to empty string.

Are you willing to submit a PR?
Yes

Activity

  1. trivikr commented on Jan 31, 2023

    @trivikr
    ContributorAuthor

    I think the implementation can be as simple as

    -    if (cache && isCacheFeatureAvailable()) {
    +    const packageManagerFromManifest = getNameFromPackageManagerField();
    +    if ((cache !== "" || packageManagerFromManifest) && isCacheFeatureAvailable()) {
           const cacheDependencyPath = core.getInput('cache-dependency-path');
    -      await restoreCache(cache, cacheDependencyPath);
    +      const packageManager = cache !== "" ? cache : packageManagerFromManifest;
    +      await restoreCache(packageManager, cacheDependencyPath);
         }

    where getNameFromPackageManagerField returns the package manager name from packageManager field. That is:

    • yarn if the value is yarn@X.Y.Z
    • pnpm is the value is pnpm@X.Y.Z
    • undefined otherwise

    Other package managers can be added, once they are supported

  2. MaksimZhukov commented on Feb 1, 2023

    @MaksimZhukov
    Contributor

    Hello @trivikr ! Thank you for the suggested idea!
    We will consider adding this feature and will let you know as soon as we have any decision.

  3. Airkro commented on Sep 16, 2023

    @Airkro

    This is what I doing before this feature landed:

    jobs:
      steps:
        - name: Checkout
          uses: actions/checkout@v3
    
        - name: Detect package manager
          shell: bash
          run: node -p "'PACKAGE_MANAGER='+require('./package.json').packageManager?.split?.('@')?.[0]" >> "$GITHUB_ENV"
    
        - name: Install pnpm
          run: corepack enable pnpm
          if: ${{ env.PACKAGE_MANAGER == 'pnpm' }}
          # install pnpm before setup-node
    
        - name: Setup node
          uses: actions/setup-node@v3
          with:
            node-version: lts
            cache: ${{ env.PACKAGE_MANAGER }}
  4. trivikr commented on Mar 25, 2025

    @trivikr
    ContributorAuthor

    Revisiting this, as devEngines has now appeared as a standard in Node.js ecosystem.
    It'll be better to track devEngines.packageManager instead of (or in addition to) packageManager field.

    It's implemented by npm@10.9.0 as well as corepack@0.32.0.

  5. v-HarithaVattikuti commented on Aug 22, 2025

    @v-HarithaVattikuti
    Contributor

    Hello Everyone,
    We’ve raised PR #1348 to address the above request. Please validate it against the requirement, and confirm to proceed further.
    Feel free to reach out if you have any questions or need further clarification.

  6. v-priya-kinthali commented on Aug 28, 2025

    @v-priya-kinthali
    Contributor

    Hello everyone👋,
    The requested feature has now been implemented and the related PR#1348 has been merged. This update supports enabling caching when a valid package manager field is detected. To disable this automatic caching, set package-manager-cache to false.
    To utilise this feature, you can use the actions/setup-node@main branch in your workflows for now, and the changes will be reflected in the upcoming releases.
    We’ll be closing this issue. Please feel free to reach out if you have any further concerns or need additional assistance.
    Thanks again for your suggestion and feedback!

  7. kleinfreund commented on Sep 4, 2025

    @kleinfreund

    Revisiting this, as devEngines has now appeared as a standard in Node.js ecosystem.
    It'll be better to track devEngines.packageManager instead of (or in addition to) packageManager field.

    Was there a particular reason not to look at devEngines in https://github.com/actions/setup-node/pull/1348/files#diff-4fab5baaca5c14d2de62d8d2fceef376ddddcc8e9509d86cfa5643f51b89ce3dR139 (e.g. via const pm = packageJson.devEngines?.packageManager ?? packageJson.packageManager;) first?

  8. v-priyagupta108 commented on Sep 22, 2025

    @v-priyagupta108
    Contributor

    Hello 👋,

    We’re planning to update this feature so that caching is automatically enabled for npm, while caching will be disabled by default for other package managers (pnpm and yarn). This adjustment is being made based on recent issues with pnpm and yarn integration. For more background, please see issue #1357.
    Our goal with this change is to ensure reliability and prevent failures like those reported for pnpm and yarn. If you have any concerns or suggestions, please share your feedback!

    Thank you for your understanding and support.

  9. MikeMcC399 commented on Sep 26, 2025

    @MikeMcC399
    Contributor

    @priyagupta108

    Enabling Yarn caching through actions/setup-node@v5 by default will break CI in nodejs/corepack if updated from v4 to v5. This GitHub Actions CI sets the environment variable YARN_ENABLE_GLOBAL_CACHE: false.

    In a test branch https://github.com/MikeMcC399/corepack/tree/test/setup-node-v5 I simply updated from actions/setup-node@v4 to actions/setup-node@v5 and the workflow failed with:

    Run actions/setup-node@v5
    Attempt to resolve LTS alias from manifest...
    (node:2071) [DEP0040] DeprecationWarning: The `punycode` module is deprecated. Please use a userland alternative instead.
    (Use `node --trace-deprecation ...` to show where the warning was created)
    Found in cache @ /opt/hostedtoolcache/node/22.19.0/x64
    Environment details
    /usr/local/bin/yarn --version
    1.22.22
    /usr/local/bin/yarn cache dir
    error This project's package.json defines "packageManager": "yarn@4.9.0". However the current global version of Yarn is 1.22.22.
    
    Presence of the "packageManager" field indicates that the project is meant to be used with Corepack, a tool included by default with all official Node.js distributions starting from 16.9 and 14.19.
    Corepack must currently be enabled by running corepack enable in your terminal. For more information, check out https://yarnpkg.com/corepack.
    Error: error This project's package.json defines "packageManager": "yarn@4.9.0". However the current global version of Yarn is 1.22.22.
    
    Presence of the "packageManager" field indicates that the project is meant to be used with Corepack, a tool included by default with all official Node.js distributions starting from 16.9 and 14.19.
    Corepack must currently be enabled by running corepack enable in your terminal. For more information, check out https://yarnpkg.com/corepack.
    

    After disabling cache in a different branch https://github.com/MikeMcC399/corepack/tree/test/setup-node-v5-no-cache with

          - uses: actions/setup-node@v5
            with:
              node-version: lts/*
              package-manager-cache: false

    the workflow succeeded.

  10. v-aparnajyothi-y commented on Sep 26, 2025

    @v-aparnajyothi-y
    Contributor

    Hello Everyone, As part of PR #1374, we introduce detection support for devEngines.packageManager, enabling automatic caching for npm dependencies. This update will be included in the upcoming patch release of v5. We hope this enhancement helps streamline workflows and improve consistency across your projects.

    Your feedback and suggestions are appreciated. Please take a look and share your thoughts.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

feature requestNew feature or request to improve the current logic

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions