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

V9 doesn't work after install #1366

Closed
1 task done
keyten opened this issue Jan 27, 2024 · 15 comments
Closed
1 task done

V9 doesn't work after install #1366

keyten opened this issue Jan 27, 2024 · 15 comments

Comments

@keyten
Copy link

keyten commented Jan 27, 2024

Basically what I did:

npm install --save-dev husky

and then:

npx husky init

file:///Users/.../project/node_modules/husky/bin.js:10
  ;(d.scripts ||= {}).prepare = 'husky'
              ^^^

SyntaxError: Unexpected token '||='
    at Loader.moduleStrategy (internal/modules/esm/translators.js:149:18)
    at async link (internal/modules/esm/module_job.js:67:21)

Troubleshoot

If you're migrating from husky 4, see:
https://typicode.github.io/husky/migrate-from-v4.html

Context

  • OS X, normal OSX terminal
@keyten
Copy link
Author

keyten commented Jan 27, 2024

Here's the contents of husky/bin.js:

#!/usr/bin/env node
import f, { writeFileSync as w } from 'fs'
import i from './index.js'

let a = process.argv[2]

if (a == 'init') {
  let p = 'package.json'
  let d = JSON.parse(f.readFileSync(p))
  ;(d.scripts ||= {}).prepare = 'husky'
  w('package.json', JSON.stringify(d, null, /\t/.test() ? '\t' : 2) + '\n')
  process.stdout.write(i())
	try { f.mkdirSync('.husky') } catch {}
  w('.husky/pre-commit', process.env.npm_config_user_agent.split('/')[0] + ' test\n')
  process.exit()
}

let d = c => console.error(`${c} command is deprecated`)
if (['add', 'set', 'uninstall'].includes(a)) { d(a); process.exit(1) }
if (a == 'install') d(a)

process.stdout.write(i(a == 'install' ? undefined : a))

@typicode
Copy link
Owner

Hi what's your version of Node?

@zahedul
Copy link

zahedul commented Jan 27, 2024

Hi what's your version of Node?

node 20 for me

@typicode
Copy link
Owner

Weird that syntax should be supported

@jomardiago
Copy link

Experiencing the same, build failing in github actions. Had to go back to 8.0.3. Node version is 18 though, upgraded to 20 and still the same error.

@typicode
Copy link
Owner

What's your exact Node version? I'll give it a try locally later.

I can't seem to reproduce the issue with GitHub Actions:
https://github.com/typicode/husky/blob/main/test/7_init.sh#L6 husky initexits correctly and doesn't produce errors https://github.com/typicode/husky/actions/runs/7697435100/job/20974451644#step:6:114

Also husky init should be run locally, once, to setup husky. It shouldn't run in the CI.

@jomardiago
Copy link

What's your exact Node version? I'll give it a try locally later.

I can't seem to reproduce the issue with GitHub Actions: https://github.com/typicode/husky/blob/main/test/7_init.sh#L6 husky initexits correctly and doesn't produce errors https://github.com/typicode/husky/actions/runs/7697435100/job/20974451644#step:6:114

Also husky init should be run locally, once, to setup husky. It shouldn't run in the CI.

Hi @typicode, sorry for the late reply, node v18.15.0, but I think I know what my issue is, in my package.json I still have
"prepare": "npx husky install".

Should I replace this with "npx husky init" instead?

image

@typicode
Copy link
Owner

Replace it with husky. No need for npx or install anymore

@typicode
Copy link
Owner

And thanks for the details, I'm still not sure why there's a syntax error

@KresimirCosic
Copy link

KresimirCosic commented Feb 1, 2024

yeah this doesn't work anymore, so my structure of project is this:

repo-root/package/.husky

I had this and it worked (filtering out when CI):
"prepare": "if [[ ! \"${HUSKY}\" = \"0\" ]]; then cd .. && husky install package/.husky | chmod ug+x package/.husky/*; fi"

I changed it to be husky@latest, to just husky (removing install) it doesn't work anymore, error reads:

      throw er; // Unhandled 'error' event
      ^

Error: write EPIPE
    at afterWriteDispatched (node:internal/stream_base_commons:160:15)
    at writeGeneric (node:internal/stream_base_commons:151:3)
    at Socket._writeGeneric (node:net:952:11)
    at Socket._write (node:net:964:8)
    at writeOrBuffer (node:internal/streams/writable:447:12)
    at _write (node:internal/streams/writable:389:10)
    at Writable.write (node:internal/streams/writable:393:10)
    at file://path/to/node_modules/husky/bin.js:22:16
    at ModuleJob.run (node:internal/modules/esm/module_job:217:25)
    at async ModuleLoader.import (node:internal/modules/esm/loader:316:24)
Emitted 'error' event on Socket instance at:
    at emitErrorNT (node:internal/streams/destroy:151:8)
    at emitErrorCloseNT (node:internal/streams/destroy:116:3)
    at process.processTicksAndRejections (node:internal/process/task_queues:82:21) {
  errno: -32,
  code: 'EPIPE',
  syscall: 'write'
}

Node.js v20.8.1```

@typicode
Copy link
Owner

typicode commented Feb 1, 2024

"prepare": "cd .. && husky package/.husky is sufficient, the rest can be removed
https://typicode.github.io/husky/how-to.html#project-not-in-git-root-directory

@KresimirCosic
Copy link

OK I removed the shell code, I used it to check if it's in CI environment to not run the install script. Do you know any better way? I remember finding some package is-ci that people use a ton as well

@jomardiago
Copy link

Hi @typicode , changing prepare script from "npx husky install" to just "husky" did not solve my issue. Still encoutering the same error in our github workflow. NodeJS is updated however.

@typicode
Copy link
Owner

typicode commented Feb 2, 2024

Sorry, I don't know why it's failing. Here I'm using husky v9 in a GitHub Action:
https://github.com/typicode/json-server/actions/runs/7726152327/job/21061864912#step:4:7

There's no error.

Is your repo public?

@jomardiago
Copy link

Sorry, I don't know why it's failing. Here I'm using husky v9 in a GitHub Action: https://github.com/typicode/json-server/actions/runs/7726152327/job/21061864912#step:4:7

There's no error.

Is your repo public?

Hi @typicode , sorry for the late reply, sorry it is private, will just move back to version 8 for now, then will investigate further. I'll let you know once I either fix it or found something. Thank you for your time.

gcko added a commit to gcko/husky that referenced this issue Feb 5, 2024
Fix typicode#1366 by adding documentation to `how-to.md`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants