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.
Running cy.exec even on the first time, not just every time after the first time
"cypress": "^3.8.1" in package json as a dev dependency
macOS catalina
@zsolt-dev I'm not able to reproduce this error. Please provide the exact test code.
package.json script
"scripts": {
"foo": "echo foo"
},
it('exec npm', () => {
cy.exec('npm run foo').then((obj) => {
cy.log(obj.code)
cy.log(obj.stdout)
cy.log(obj.stderr)
})
})

I saw this issue and noticed OP is using zsh as their shell (which is the new default on macOS catalina). I'm also using zsh on ubuntu and had the same issue. I don't have the issue when switching my default shell back to bash with chsh -s /bin/bash.
I also use zsh as my shell (on Catalina) and am unable to reproduce the issue from the code I pasted above.
Unfortunately we'll have to close this issue if no reproducible example is provided. Please provide the test code to run to reproduce this error.
This is an issue I am also running into with Catalina, zsh, and executing npm commands through Cypress. I am also using nvm which I think _might_ be part of it? Haven't yet been able to find a way to reproduce it since it seems dependent on how Catalina is setup.
If I run npx cypress run personally from the command line then my cy.exec('npm run foo') command works. But if Cypress isn't executed directly from the commandline but instead through a script, it can't find npm or npx like OP's thread.
"scripts": {
"foo": "echo foo"
},
Startup script I am using:
import { exec } from 'child_process'
exec('npx cypress run', (error, stdout, stderr) => {
if (error) console.error(error)
console.log(stdout)
console.error(stderr)
})
Cypress test:
it('should echo foo', () => {
cy.exec('npm run foo')
})
Gives me an error env: node: No such file or directory from inside the Cypress test
@jennifer-shehane updated my description with minimal code to reproduce as best I can.
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.
We use nvm at Cypress ourselves, and haven't encountered this error before.
This is essentially the exec code: https://github.com/cypress-io/cypress/blob/develop/packages/server/lib/exec.js#L18:L18
Funny as it sounds. I get this error as well when the failing tests runs for the first time only.
I was able to get around this with setting a retries option on the test. But it still doesn't make sense as I need to run my custom command in beforeAll or beforeEach hook.
and yes I also use nvm and zsh and I tried both:
cypress openIn both environments the problem is the same: It only works on second try.
Hopefully my little hack will work by executing the command before all tests, let it retry and pass. and then it shouldn't require setting retries anymore.
I have this same issue too.
I'm using Ubuntu, ZSH and nvm.
and also no problem when I'm running cypress directly (eg. ./node_modules/.bin/cypress run) it's only reproducable via npm scritp
Most helpful comment
Just chiming in to say we were debugging something similar today…
Context:
We have a command (
prep), frequently run in abeforeAll, that looked something like this: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
sleepworks every time: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.execworks.