nodejs / snap

Node.js snap source and updater
MIT License
165 stars 38 forks source link

Cypress fails to run npm postinstall #68

Open MikeMcC399 opened 1 month ago

MikeMcC399 commented 1 month ago

Issue

Attempting to install the npm module cypress fails at the postinstall stage using npm.

npm info run cypress@13.13.0 postinstall node_modules/cypress node index.js --exec install npm info run cypress@13.13.0 postinstall { code: 1, signal: null }

This issue does not occur if Node.js is not installed via snap, for instance via nvm.

Steps to reproduce

On Ubuntu:

mkdir cy-test
cd cy-test
git init
npm init -y
npm install cypress -D

Note: If Cypress has been previously fully installed, then you need to execute the following to delete the Cypress binary from its cache:

npx cypress cache clear

or

rm -rf ~/.cache/Cypress

Environment

Ubuntu 22.04.4 LTS Node.js v20.14.0 8723 20/stable installed with snap

Continues to be reproducible with node 20.15.1 8864 20/stable (bundled npm version 10.7.0)

Logs

Install

npm install cypress -D --ddd
...
npm info run cypress@13.13.0 postinstall node_modules/cypress node index.js --exec install
npm info run cypress@13.13.0 postinstall { code: 1, signal: null }
npm verbose stack Error: command failed
npm verbose stack     at ChildProcess.<anonymous> (/snap/node/8723/lib/node_modules/npm/node_modules/@npmcli/promise-spawn/lib/index.js:53:27)
npm verbose stack     at ChildProcess.emit (node:events:519:28)
npm verbose stack     at maybeClose (node:internal/child_process:1105:16)
npm verbose stack     at ChildProcess._handle.onexit (node:internal/child_process:305:5)
npm verbose pkgid cypress@13.13.0
npm verbose cwd /home/mike/github/cy-e2e-default
npm verbose Linux 6.5.0-41-generic
npm verbose node v20.14.0
npm verbose npm  v10.7.0
npm error code 1
npm error path /home/mike/github/cy-e2e-default/node_modules/cypress
npm error command failed
npm error command sh -c node index.js --exec install
npm verbose exit 1
npm verbose code 1

snap

$ snap list node
Name  Version  Rev   Tracking   Publisher  Notes
node  20.14.0  8723  20/stable  iojs✓      classic

Workaround

Workaround - split postinstall phase

npm install cypress -D --ignore-scripts
npx cypress install

npx cypress install is the equivalent of node ./node_modules/cypress/index.js --exec install

Workaround - install Node.js with nvm

Install Node.js on Ubuntu with nvm according to instructions on https://nodejs.org/en/download/package-manager

# installs nvm (Node Version Manager)
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.7/install.sh | bash

# download and install Node.js (you may need to restart the terminal)
nvm install 20

# verifies the right Node.js version is in the environment
node -v # should print `v20.15.0`

# verifies the right NPM version is in the environment
npm -v # should print `10.7.0`
MikeMcC399 commented 1 month ago

I also found that running

npm install cypress --foreground-scripts

is successful, so it is something connected to running an npm lifecycle script in the background using node/npm from snap on Ubuntu.

MikeMcC399 commented 1 month ago

This issue only affects npm installed via snap node. Using Yarn installed via snap node works correctly.

The following sequence completes successfully:

yarn init -y
yarn add cypress -D
yarn cypress cache list
MikeMcC399 commented 1 month ago

Also reproducible installing using npm with:

Ubuntu 24.04 LTS node (22/stable) 22.4.1 from OpenJS Foundation (iojs✓) installed

MikeMcC399 commented 1 month ago

@rvagg

Will somebody be able to give some feedback on this issue? It may need a warning in the Cypress documentation, but I'd like to understand the root cause first before proposing a documentation update.

snap is one of the recommended node package managers on https://nodejs.org/en/download/package-manager/all

rvagg commented 1 month ago

Sorry, I don't have clear advice for you on this one, snaps have trouble doing child process execs because they are already executed in constrained environments; I opt to not use node from a snap if I want to not bump into these problems. I wish I could get to the bottom of it and be able to either provide advice or fix something, but it's not obvious to me where to start digging and I don't have the time or reason to dig deeper, sorry.

MikeMcC399 commented 1 month ago

@rvagg

Thanks for your valuable feedback and my apologies for pinging you!

I'm going to assume that the problem is inherent to snap and I'll propose a documentation update to Cypress. I don't personally use snap as a package manager for node (I prefer n & nvm), but some other Cypress users have hit this issue and I'm trying to make sure that other users won't fall into the same trap.

MikeMcC399 commented 1 month ago

Now documented on

https://docs.cypress.io/guides/getting-started/installing-cypress#npm-install