Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

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

Closed
zsolt-dev opened this issue Jan 2, 2020 · 23 comments
Closed

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

zsolt-dev opened this issue Jan 2, 2020 · 23 comments

Comments

@zsolt-dev
Copy link

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

@jennifer-shehane
Copy link
Member

@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)
  })
})

Screen Shot 2020-01-03 at 12 59 36 AM

@jennifer-shehane jennifer-shehane added the stage: needs information Not enough info to reproduce the issue label Jan 2, 2020
@amkoehler
Copy link

amkoehler commented Jan 14, 2020

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.

@jennifer-shehane
Copy link
Member

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.

@racedale
Copy link

racedale commented Apr 22, 2020

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

@racedale
Copy link

@jennifer-shehane updated my description with minimal code to reproduce as best I can.

@dylnclrk
Copy link

dylnclrk commented May 14, 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.

@jennifer-shehane
Copy link
Member

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

@michalvankodev
Copy link

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:

  1. running test via Module API
  2. running tests via cypress open

In 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.

@mlewando90
Copy link

mlewando90 commented Nov 9, 2020

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

@dlcoffee
Copy link

dlcoffee commented Jan 4, 2021

i had the same issue as well. i was on nvm 0.33.11. i upgraded to the latest version (0.37.2 as of writing) and that seems to have solved my problem.

@jennifer-shehane
Copy link
Member

Are any of you setting nodeVersion in your Cypress configuration, like cypress.json? Let us know if you are or are not setting this and still experiencing the issue.

@dlcoffee
Copy link

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

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

@GradedJestRisk
Copy link

@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

@joe1248
Copy link

joe1248 commented Feb 22, 2021

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

@hvaandres
Copy link

I'm having the same issue, and maybe this is a good reference. Github Repo

@zsolt-dev
Copy link
Author

updating the nvm to the version solved the issue for me.

Thanks to @dlcoffee for providing the fix in the comment.

@jennifer-shehane jennifer-shehane removed the stage: needs information Not enough info to reproduce the issue label Jun 16, 2021
@clazette
Copy link

@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)
  })
})
Screen Shot 2020-01-03 at 12 59 36 AM

I am encountering this issue as well. I have attempted your example:
image

@clazette
Copy link

I've attempted to change the path but that doesn't appear to help. What am I missing?

image

@clazette
Copy link

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

@AlienLifeForm-UMA
Copy link

AlienLifeForm-UMA commented Nov 2, 2022

I have the same issue and have to add "source ~/.zshrc" to all exec commands for nvm/Node to work. So it seems that Cypress doesn't actually run the zshrc file even when using zsh? I checked using echo $0 and it is using zsh.

@alexsegura
Copy link

I think I have almost the same problem under Mac OSX, using Cypress standalone / direct download.

I'm using cy.exec() to run a Docker Compose command (i.e cy.exec('docker compose exec -T ...')).

Depending on how Cypress is launched, the $PATH is not the same.

  • When it's launched via Spotlight or double-click, it does not contain /usr/local/bin (that's where the docker binary is located).
  • When it's launched via command line open -a /Applications/Cypress.app, it contains /usr/local/bin.

@wojciechbialasek
Copy link

@alexsegura I also facing the same issue, have you solved it?

@gbutt-tr
Copy link

I am using this npm script to open cypress:
"cypress:open": "dotenv -v TZ=Etc/UTC -- cypress open"

Thanks to the comments above, I was able to fix this issue by adding SHELL=/bin/bash to the script:
"cypress:open": "dotenv -v TZ=Etc/UTC -v SHELL=/bin/bash -- cypress open"

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests