Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

[BUG] npx fails with "could not find executable" when running husky in v7 #1845

Closed
csvn opened this issue Sep 22, 2020 · 26 comments
Closed

[BUG] npx fails with "could not find executable" when running husky in v7 #1845

csvn opened this issue Sep 22, 2020 · 26 comments
Assignees
Labels
Bug thing that needs fixing Release 7.x work is associated with a specific npm 7 release

Comments

@csvn
Copy link

csvn commented Sep 22, 2020

The latest version of husky is broken in npm v7. I'm not sure if this regression is intended or not, i.e. if Husky needs to be updated or npm needs a bugfix.

Current Behavior:

~ npx husky-run pre-commit
npm ERR! could not determine executable to run

npm ERR! A complete log of this run can be found in:
...

Expected Behavior:

~ npx husky-run pre-commit
husky > pre-commit (node v14.10.1)
Hello World!
...

Steps To Reproduce:

  1. Create a new empty folder and run npm init -y inside it
  2. Install husky with npm i -D husky
  3. Create a simple config, e.g . echo '{ "hooks": { "pre-commit": "echo Hello World!" } }' > .huskyrc.json
  4. Run npx husky-run pre-commit
  5. npm v7 will fail with "could not determine executable", while npm v6 works

Environment:

  • OS: Ubuntu 20.04 or Windows 10 v2004
  • Node: 14.10.1
  • npm: 7.0.0-beta.11
@csvn csvn added Bug thing that needs fixing Needs Triage needs review for next steps Release 7.x work is associated with a specific npm 7 release labels Sep 22, 2020
@rafgraph
Copy link

This might be related to #1834

@csvn
Copy link
Author

csvn commented Sep 24, 2020

@rafgraph On windows, I had no issues for hooks to be installed with Husky. When I used WSL on Ubuntu though, I had to use sudo for hooks to be installed, otherwise I had errors in the npm install log. So I don't think this is strictly related, since the error above can be reproduced by simply running npx husky-run pre-commit without hooks being installed at all.

@darcyclarke
Copy link
Contributor

darcyclarke commented Sep 25, 2020

@csvn can you try installing the latest beta & seeing if you can repro? npm i -g npm@next-7. If you're still experiencing this, we'd love the debug.log

@darcyclarke darcyclarke removed the Needs Triage needs review for next steps label Sep 25, 2020
@darcyclarke darcyclarke added this to the OSS - Sprint 16 milestone Sep 25, 2020
@csvn
Copy link
Author

csvn commented Sep 25, 2020

Hi @darcyclarke. Attached the output for the debug.log for the reproduction steps in the description.

0 verbose cli [
0 verbose cli   '/usr/local/bin/node',
0 verbose cli   '/usr/local/share/npm-global/lib/node_modules/npm/bin/npm-cli.js',
0 verbose cli   'exec',
0 verbose cli   '--',
0 verbose cli   'husky-run',
0 verbose cli   'pre-commit'
0 verbose cli ]
1 info using npm@7.0.0-beta.12
2 info using node@v14.10.1
3 timing config:load:defaults Completed in 2ms
4 timing config:load:file:/usr/local/share/npm-global/lib/node_modules/npm/npmrc Completed in 1ms
5 timing config:load:builtin Completed in 1ms
6 timing config:load:cli Completed in 1ms
7 timing config:load:env Completed in 0ms
8 timing config:load:file:/workspaces/npm-husky-test/.npmrc Completed in 0ms
9 timing config:load:project Completed in 1ms
10 timing config:load:file:/root/.npmrc Completed in 0ms
11 timing config:load:user Completed in 0ms
12 timing config:load:file:/usr/local/etc/npmrc Completed in 1ms
13 timing config:load:global Completed in 1ms
14 timing config:load:cafile Completed in 0ms
15 timing config:load:validate Completed in 0ms
16 timing config:load:setEnvs Completed in 1ms
17 timing config:load Completed in 7ms
18 verbose npm-session 53798df1482fef51
19 timing npm:load Completed in 13ms
20 http fetch GET 200 https://registry.npmjs.org/husky-run 832ms
21 timing command:exec Completed in 840ms
22 verbose stack Error: could not determine executable to run
22 verbose stack     at getBinFromManifest (/usr/local/share/npm-global/lib/node_modules/npm/lib/exec.js:210:23)
22 verbose stack     at exec (/usr/local/share/npm-global/lib/node_modules/npm/lib/exec.js:98:15)
23 verbose cwd /workspaces/npm-husky-test
24 verbose Linux 4.19.128-microsoft-standard
25 verbose argv "/usr/local/bin/node" "/usr/local/share/npm-global/lib/node_modules/npm/bin/npm-cli.js" "exec" "--" "husky-run" "pre-commit"
26 verbose node v14.10.1
27 verbose npm  v7.0.0-beta.12
28 error could not determine executable to run
29 verbose exit 1

I ran this within a Docker Devcontainer and WSL in VSCode. Environment info:

root@cc760b824422:/workspaces/npm-husky-test# npm -v
7.0.0-beta.12
root@cc760b824422:/workspaces/npm-husky-test# node -v
v14.10.1

@csvn
Copy link
Author

csvn commented Sep 25, 2020

This is the same for Windows, npm 7.0.0-beta.12 node v14.8.0:

0 verbose cli [
0 verbose cli   'C:\\Program Files\\nodejs\\node.exe',
0 verbose cli   'C:\\Program Files\\nodejs\\node_modules\\npm\\bin\\npm-cli.js',
0 verbose cli   'exec',
0 verbose cli   '--',
0 verbose cli   'husky-run',
0 verbose cli   'pre-commit'
0 verbose cli ]
1 info using npm@7.0.0-beta.12
2 info using node@v14.8.0
3 timing config:load:defaults Completed in 1ms
4 timing config:load:file:C:\Program Files\nodejs\node_modules\npm\npmrc Completed in 0ms
5 timing config:load:builtin Completed in 0ms
6 timing config:load:cli Completed in 1ms
7 timing config:load:env Completed in 1ms
8 timing config:load:file:C:\Users\Christian\Dev\@apptus\esales-fashion\.npmrc Completed in 1ms
9 timing config:load:project Completed in 1ms
10 timing config:load:file:C:\Users\Christian\.npmrc Completed in 1ms
11 timing config:load:user Completed in 1ms
12 timing config:load:file:C:\Program Files\nodejs\etc\npmrc Completed in 0ms
13 timing config:load:global Completed in 0ms
14 timing config:load:cafile Completed in 0ms
15 timing config:load:validate Completed in 0ms
16 timing config:load:setEnvs Completed in 1ms
17 timing config:load Completed in 6ms
18 verbose npm-session 3e0f43b059d2f137
19 timing npm:load Completed in 15ms
20 http fetch GET 200 https://registry.npmjs.org/husky-run 85ms
21 timing command:exec Completed in 95ms
22 verbose stack Error: could not determine executable to run
22 verbose stack     at getBinFromManifest (C:\Program Files\nodejs\node_modules\npm\lib\exec.js:210:23)
22 verbose stack     at exec (C:\Program Files\nodejs\node_modules\npm\lib\exec.js:98:15)
23 verbose cwd C:\Users\Christian\Dev\@apptus\esales-fashion
24 verbose Windows_NT 10.0.19041
25 verbose argv "C:\\Program Files\\nodejs\\node.exe" "C:\\Program Files\\nodejs\\node_modules\\npm\\bin\\npm-cli.js" "exec" "--" "husky-run" "pre-commit"
26 verbose node v14.8.0
27 verbose npm  v7.0.0-beta.12
28 error could not determine executable to run
29 verbose exit 1

@nlf
Copy link
Contributor

nlf commented Oct 1, 2020

the issue here is that npx thinks you're trying to run husky-run the package rather than husky-run the bin script that's part of husky.

what you'll want to do to run the husky-run script within husky itself, is

npx -p husky husky-run

which informs npx which package contains the bin, and I can confirm works correctly

@rafgraph
Copy link

rafgraph commented Oct 2, 2020

I get this same error could not determine executable to run when running husky as a pre-commit hook. Error log attached.

2020-10-02T00_36_01_041Z-debug.log

husky was installed with npm v6 (because of #1834), the pre-commit hook successfully setup. It is setup in package.json to run lint-staged as described in prettier docs. This works fine with npm v6

  "husky": {
    "hooks": {
      "pre-commit": "lint-staged"
    }
  },
  "lint-staged": {
    "src/**/*": "prettier --write --ignore-unknown"
  }

@csvn
Copy link
Author

csvn commented Oct 2, 2020

@nlf running npx husky-run is just for illustrating the problem. The real issue is that the hooks running with Husky fails when Husky executes them. So it is really the package that needs updating, if this issue is "working as intended". npx with npm v6 managed to find /node_modules/.bin/husky-run and run correctly. So this is a breaking change, and I was just curious if this is intended.

So in my mind, there are two options:

  • Ensure that npx <command> can find scripts in /node_modules/.bin if they match first
  • Update Husky so that it works with npm v7

I just wanted clarity which option is the right one, before opening an issue on Husky with this.

@Coridyn
Copy link

Coridyn commented Oct 6, 2020

To add another data point, I think I'm running into the same issue with the vue-cli-service bin script (which works in npm@6).

$ npx vue-cli-service serve
npm ERR! code E404
npm ERR! 404 Not Found - GET https://registry.npmjs.org/vue-cli-service - Not found
npm ERR! 404
npm ERR! 404  'vue-cli-service@latest' is not in the npm registry.
npm ERR! 404 You should bug the author to publish it (or use the name yourself!)
npm ERR! 404
npm ERR! 404 Note that you can also install from a
npm ERR! 404 tarball, folder, http url, or git url.

$ npx vue serve
npm ERR! could not determine executable to run

Based on nlf's comment there is a workaround until this is fixed in npm@7:

$ npx -p @vue/cli-service vue-cli-service serve

@nlf
Copy link
Contributor

nlf commented Oct 9, 2020

we published v7.0.0-rc.4 today that restores the old behavior of npx with regards to already installed scripts and should resolve these issues.

you can update with npm i -g npm@next-7 and let us know how it goes!

@csvn
Copy link
Author

csvn commented Oct 12, 2020

Thanks @nlf! I can verify that my Husky hooks now works again, same as with npm v6.

@csvn csvn closed this as completed Oct 12, 2020
@hanifmhd
Copy link

Hi @nlf i still got error using npm v7.4.0

@nlf
Copy link
Contributor

nlf commented Jan 26, 2021

please open a new issue and be sure to fill out the template and i'll see if i can help figure out what's going on

@tomalexhughes
Copy link

Can confirm behaviour is working in 7.5.2 however I had to run npm rebuild to re-install husky's hooks. This may be helpful for you @hanifmhd

@vijaycs85
Copy link

I got this issue with npx postcss and fixed it by removing the dist directory and running npx postcss again.

@piotros
Copy link

piotros commented Feb 24, 2021

I ran into the same bug after upgrading husky from version 4 to 5.

Installing new version of package wasn't enough. The solution to fix this issue was to migrate husky to newer version.
There's a helpful tool for this task: https://github.com/typicode/husky-4-to-5

@moshest
Copy link

moshest commented Mar 1, 2021

Thanks @piotros. I just follow the instructions here and fix it on my end as well:
https://typicode.github.io/husky/#/?id=locally-installed-binaries

@cdefy
Copy link

cdefy commented Apr 7, 2021

the issue here is that npx thinks you're trying to run husky-run the package rather than husky-run the bin script that's part of husky.

what you'll want to do to run the husky-run script within husky itself, is

npx -p husky husky-run

which informs npx which package contains the bin, and I can confirm works correctly

Hi there,

I hope I'm not making a mistake by posting here but the quoted answer is for me the closest to a solution.

I'm pretty sure you got that right because if I check the log it said "http fetch GET 304 https://registry.npmjs.org/husky-run 201ms" so I reckon it is looking for a package instead of running the script.

But I'm using Nuxt and I'm not sure how I can fix this as the solution offered is for the npm cli. If anyone has an idea it will be really appreciated.

Thanks!

EDIT : after more research, I found this StackOverFlow thread : https://stackoverflow.com/questions/66246587/how-to-fix-error-not-found-husky-run-when-committing-new-code
So I tried :

rm -rf .git/hooks
npm install

Seems to work OK now. 👍

@3mmali
Copy link

3mmali commented May 3, 2021

@nlf running npx husky-run is just for illustrating the problem. The real issue is that the hooks running with Husky fails when Husky executes them. So it is really the package that needs updating, if this issue is "working as intended". npx with npm v6 managed to find /node_modules/.bin/husky-run and run correctly. So this is a breaking change, and I was just curious if this is intended.

So in my mind, there are two options:

  • Ensure that npx <command> can find scripts in /node_modules/.bin if they match first
  • Update Husky so that it works with npm v7

I just wanted clarity which option is the right one, before opening an issue on Husky with this.

This was my issue! I had recently moved to a new machine and had forgotten to re-install npx.

@VimalArjunan51437
Copy link

#while create new react app i am fcaing this issue

i have tried all command given above.

----->npx install create-react-app zoho

after given above cmd . i am facing below error

npm ERR! could not determine executable to run

npm ERR! A complete log of this run can be found in:
npm ERR! C:\Users\Admin\AppData\Local\npm-cache_logs\2021-06-08T02_41_23_166Z-debug.log

@senjufy
Copy link

senjufy commented Jul 27, 2021

I used npx tsc --init. Worked for me. Im on a linux machine, if that fact matters. I also have typescript installed globally with npm.

@vedraan
Copy link

vedraan commented Sep 14, 2021

the issue here is that npx thinks you're trying to run husky-run the package rather than husky-run the bin script that's part of husky.
what you'll want to do to run the husky-run script within husky itself, is

npx -p husky husky-run

which informs npx which package contains the bin, and I can confirm works correctly

Hi there,

I hope I'm not making a mistake by posting here but the quoted answer is for me the closest to a solution.

I'm pretty sure you got that right because if I check the log it said "http fetch GET 304 https://registry.npmjs.org/husky-run 201ms" so I reckon it is looking for a package instead of running the script.

But I'm using Nuxt and I'm not sure how I can fix this as the solution offered is for the npm cli. If anyone has an idea it will be really appreciated.

Thanks!

EDIT : after more research, I found this StackOverFlow thread : https://stackoverflow.com/questions/66246587/how-to-fix-error-not-found-husky-run-when-committing-new-code
So I tried :

rm -rf .git/hooks
npm install

Seems to work OK now. 👍

This is NOT a good way to fix it.

In fact you will completely remove all git hooks you have setup, so naturally there will be nothing to run and thus it will produce no error.

@ljharb
Copy link
Contributor

ljharb commented Sep 14, 2021

Doesn't that indicate the problem is the hooks themselves, and not npm?

@Samuel-B-D
Copy link

Samuel-B-D commented Sep 29, 2021

Add "prepare": "husky install" as a script to package.json to fix it for everyone else when they will run npm install, then run npm run prepare to fix if for yourself.

( Note: this is done automatically for you when using the migration script here : https://github.com/typicode/husky-4-to-7 )

@Moskaoud
Copy link

npm i yarn
yarn install
then run command again npx husky-run pre-commit

@kyawsan834
Copy link

not working

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug thing that needs fixing Release 7.x work is associated with a specific npm 7 release
Projects
None yet
Development

No branches or pull requests