pnpm: postinstall script not run if project restored

Verify latest release

  • I verified that the issue exists in the latest pnpm release

pnpm version

10.4.1

Which area(s) of pnpm are affected? (leave empty if unsure)

CLI

Link to the code that reproduces this issue or a replay of the bug

https://github.com/MikeMcC399/cypress-test-tiny/tree/pnpm-10-corepack-install

Reproduction steps

Ubuntu 24.04.2 LTS, Node.js 22.14.0 LTS using pnpm@10.4.1

corepack enable pnpm
rm -rf ~/.local/share/pnpm # clear store for fresh start

## First installation
git clone --branch pnpm-10-corepack-install https://github.com/MikeMcC399/cypress-test-tiny
cd cypress-test-tiny
pnpm install # install dependencies
pnpm cypress verify # check that all components are installed
pnpm cypress cache clear # clear cache installed by cypress postinstall

## Remove cloned repo
cd ..
rm -rf cypress-test-tiny

## Second restored installation
git clone --branch pnpm-10-corepack-install https://github.com/MikeMcC399/cypress-test-tiny # clone repo a second time
cd cypress-test-tiny
pnpm install # install dependencies
pnpm cypress verify # fails - cypress cache not installed

Describe the Bug

If an npm package with an allowed postinstall script is removed and reinstalled, the postinstall script is not run for the re-installation.

The package is allowed through pnpm.onlyBuiltDependencies configuration record of package.json.

Note: this is not a regression from pnpm 9.x. The same problem already previously existed, except all scripts were allowed anyway.

Expected Behavior

Even if a package is already present in pnpm’s store, running pnpm install should always run the postinstall script (if allowed).

Which Node.js version are you using?

v22.14.0 LTS

Which operating systems have you used?

  • macOS
  • Windows
  • Linux

If your OS is a Linux based, which one it is? (Include the version if relevant)

Ubuntu 24.04.2 LTS

About this issue

  • Original URL
  • State: open
  • Created 2 years ago
  • Reactions: 3
  • Comments: 23 (17 by maintainers)

Most upvoted comments

I see a couple of possible solutions.

We could check if there are any changes to the files of the package after running postinstall. If no, we probably can’t cache it. But this might not be reliable.

Another option would be to allow specific packages to opt-out from side-effects cache. Maybe some field in package.json, like “pnpm.bypassSideEffectsCache=true”. Related issue: https://github.com/pnpm/pnpm/issues/5271

Thanks for all the information so far, I would like to provide another data point. I am using Prisma which relies on their postinstall script to create client folder (.prisma/client), and this can’t be cached by pnpm at the moment (v10.12.1). This has caused some confusions before (issues).

As others already pointed out, I think either allowing the consumers to disable side effects cache by package, or allowing the consumed packages to indicate they are not cachable by pnpm would be helpful. Maybe a combination of both to provide the most flexibility, and start with the consumer side as it is more controllable.

@melroy89

The Cypress documentation https://docs.cypress.io/app/get-started/install-cypress#pnpm-Configuration has been updated to include instructions to disable the pnpm side effects cache.

pnpm uses a side-effects cache by default. This means that a package with a postinstall script is built one time on a machine and written to cache. Next time the cached prebuild version is installed.

@melroy89

The Cypress documentation https://docs.cypress.io/app/get-started/install-cypress#pnpm-Configuration has been updated to include instructions to disable the pnpm side effects cache.

Thanks for the update. Yeah, I can indeed confirm the workaround works. Lets hope a final fix in pnpm will be released.

@melroy89

Cypress documentation is not clear and only mention “allowlisting”

@melroy89

You can consider side-effects-cache=false a workaround, however I would expect this to need to stay with us for quite a while.

The ideal situation would be if pnpm defined a key that Cypress could add to its package.json to say it is not compatible with side effects caching, then pnpm could take account of that without any user intervention being necessary.

I think you saw that there is an issue (https://github.com/pnpm/pnpm/issues/9394) with using pnpm-workspace.yaml for the setting. We’ll need to wait for feedback on that point. Edit: This issue is now resolved.

In my tests, if Cypress hasn’t been previously installed and is not cached then the postinstall script will run (assuming it is listed in onlyBuiltDependencies or similar).

@melroy89

Which version of pnpm are you using? Are you having this problem locally or in a CI workflow? If CI, which provider is it?

Have you set side-effects-cache=false ? If yes, then try deleting the node_modules directory and re-running pnpm install

If that doesn’t work, I suggest opening a new issue to gather more details about your configuration.

We had a similar issue with puppeteer which runs a a post install scripts to install a chrome instance to $HOME/.cache/puppeteer. Beside adding

side-effects-cache=false

to .npmrc

We also had to add:

"pnpm": {
        "onlyBuiltDependencies": [
            "puppeteer"
        ]
    }

to package.json.

That’s not a good solution. You will disable it for all dependencies.

@zkochan

Thank you very much for your help!

Adding

side-effects-cache=false

to the project’s .npmrc resolves the issue. I will pass this on so that the information can be added to the Cypress documentation on https://docs.cypress.io/app/get-started/install-cypress

@zkochan

Let me look at the side-effects cache setting to see if it solves the issue.

Cypress uses postinstall to install its Electron app. This is indeed cached, however users have reported issues in the past in GitHub Actions where the pnpm store is cached separately to the Cypress cache and they can get out of sync, especially if there are parallel jobs running.

Yes it does make changes outside the Cypress package directory.

It’s described in https://docs.cypress.io/app/references/advanced-installation#Binary-cache and https://docs.cypress.io/app/continuous-integration/overview#Caching

For comparison, Yarn 4 pnp does run the postinstall script if a project is reinstalled.