cypress: possible bug in cy.exec() - command not found: npm

Current behavior:

CypressError: cy.exec('npm run db:reset') failed because the command exited with a non-zero code.

Pass {failOnNonZeroExit: false} to ignore exit code failures.

Information about the failure:
Code: 127

Stderr:
zsh:1: command not found: npm

Because this error occurred during a 'before each' hook we are skipping the remaining tests in the current suite: 'deleteUser'

After clicking on “Run all tests” (reload button) it works fine. It just does not work on the first time.

When I tried to provide PATH as an env option to cy.exec() it was doing the same thing.

Desired behavior:

Running cy.exec even on the first time, not just every time after the first time

Steps to reproduce: (app code and test code)

  1. npm run cy:open
  2. click on the test that is using cy.exec()
  3. it fails
  4. click on “Run all tests” (reload button)
  5. the same test now works fine

Versions

“cypress”: “^3.8.1” in package json as a dev dependency macOS catalina

About this issue

  • Original URL
  • State: closed
  • Created 4 years ago
  • Reactions: 6
  • Comments: 22 (4 by maintainers)

Most upvoted comments

Just chiming in to say we were debugging something similar today…

Context:

We have a command (prep), frequently run in a beforeAll, that looked something like this:

Cypress.Commands.add("prep", () => {
  cy.exec("npm run prep");
});

The first time cy.prep() was run, it would fail with the error described in this issue (Code: 127, command not found: npm). But when run before subsequent tests in the suite, it would pass.

In debugging, we saw that adding another exec command (pwd, sleep, whatever) seemed to prevent it from breaking. For example, this version with a sleep works every time:

Cypress.Commands.add("prep", () => {
  cy.exec("sleep 1");
  cy.exec("npm run prep");
});

I can’t reproduce it on my machine, only on another developer’s laptop.

She’s using Bash as her default shell and High Sierra, fwiw.

Could this be an issue with nvm (perhaps login scripts/shimming is not yet complete)? I have no idea how cy.exec works.

Changing your VS Code terminal from Bash to Powershell solves the issue.

I had a similar issue, a solution was: Doing which npm on your system may give you user/local/bin/npm. Then, in cypress, before calling npm, just do export PATH=/usr/local/bin/:$PATH

@michalvankodev, I come against a similar behaviour as yours. Mine was a dependency issue, solved on Node 14

What I found that may interest you:

  • when runinng a script for the first time throught npm, like cy.exec('npm run db:empty'); here
  • cypress used Ubuntu sh (dash) shell, not the default login shell (for me, zsh), the one you set with chsh (you can check this by inserting ps -p $$ in your npm script)

I quick-fixed it by installing Node on OS level, as I haven’t been able to install nvm on dash

So why cypress use sh instead of zsh ? The shell is picked here using $SHELL environment variables, themselves provided by shell-env dependency.

So I can’t figure why:

  • if you launch cypress using zsh ($SHELL is zsh)
  • cypress ends up reading sh in $SHELL

The answer may lay in shell-env dependency, default-shell

@jennifer-shehane i don’t have nodeVersion set in my cypress.json. it’s bare bones:

{
  "baseUrl": "http://localhost:3000/"
}