question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

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

See original GitHub issue

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

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:5
  • Comments:21 (4 by maintainers)

github_iconTop GitHub Comments

9reactions
dylnclrkcommented, Aug 28, 2020

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.

1reaction
clazettecommented, Oct 22, 2021

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

Read more comments on GitHub >

github_iconTop Results From Across the Web

cypress-io/cypress - Gitter
But I have run into a problem whit cy.exec(). ... Stderr: zsh:1: command not found: npm. The command cy.exec('echo test') works.
Read more >
exec | Cypress Documentation
cy.exec() provides an escape hatch for running arbitrary system commands, so you can take actions necessary for your test outside the scope of...
Read more >
Start script missing error when running npm start
I'm receiving this error when trying to debug my node application using the npm start command.
Read more >
Cypress Best Practices For Test Automation - LambdaTest
This blog will teach you the Cypress best practices to write reliable and high-quality test codes while performing Cypress testing.
Read more >
cypress-parallel - npm
Run your Cypress tests in parallel (locally). How it works. - Search for existing Cypress tests - Read (if exists) a weight file...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found