husky: [Docker] Husky fail to install on docker image with Node 16

Husky as devDependency with a prepare script to install the hooks according to documentation.

The prepare step fails on the node:latest container due to missing permissions.

Github Actions job is configured as follow:

  test:
    runs-on: [self-hosted, linux]
    container: mcr.microsoft.com/playwright:focal
    steps:
      - uses: actions/checkout@v2
      - run: npm ci --no-optional --no-audit --prefer-offline --progress=false
      - run: HOME=/root npm run test:playwright

This is the error ouptut from the CI:

prepare
> husky install

husky - Git hooks failed to install
/__w/project_a/project_a/node_modules/husky/lib/index.js:33
        throw e;
        ^

Error: EACCES: permission denied, mkdir '.husky/_'
    at Object.mkdirSync (node:fs:1324:3)
    at Object.install (/__w/project_a/project_a/node_modules/husky/lib/index.js:23:12)
    at Object.install (/__w/project_a/project_a/node_modules/husky/lib/bin.js:27:41)
    at Object.<anonymous> (/__w/project_a/project_a/node_modules/husky/lib/bin.js:39:22)
    at Module._compile (node:internal/modules/cjs/loader:1109:14)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1138:10)
    at Module.load (node:internal/modules/cjs/loader:989:32)
    at Function.Module._load (node:internal/modules/cjs/loader:829:14)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:76:12)
    at node:internal/main/run_main_module:17:47 {
  errno: -13,
  syscall: 'mkdir',
  code: 'EACCES',
  path: '.husky/_'
}
npm ERR! code 1
npm ERR! path /__w/project_a/project_a
npm ERR! command failed
npm ERR! command sh -c husky install

The same script runs fine on e.g. node:lts-buster container

Check out https://github.com/Th3S4mur41/bug-playwright-husky/pull/1 for an example

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Reactions: 14
  • Comments: 26 (5 by maintainers)

Commits related to this issue

Most upvoted comments

Both options: --ignore-scripts and is-ci are indeed possible workarounds have however their downside…

--ignore-script also prevents other scripts from running, which makes it unusable in some projects.

is-ci is working to, but I’d rather not add a dependency just for this purpose

A better workaround would be to skip the husky install when HUSKY is set to 0 instead of only skipping the hooks. That would address both concerns mentioned above,

In the end this is still a bug though, but unfortunately I don’t have a fix for it yet

Husky 8 now supports HUSKY=0 to disable Git hooks installation.

I moved to simple-git-hooks because of this issue. 😢

Added husky globally before installing packages, sorry thats all i got at the moment RUN npm install husky -g RUN npm install --production

I see, what about npm set-script prepare '' before?

Same issue on docker image with node 14.

@ShinJustinHolly3317

Thanks for your tip! There is a typo though in your post. The package.json section is called optionalDependencies.

Should this go into the README to say:

npm install husky -O

instead of

npm install husky -D ?

Thank you for the repo! I tried to run the GitHub actions and it seems to have been fixed: https://github.com/typicode/bug-playwright-husky/runs/5267015618?check_suite_focus=true

Maybe supporting HUSKY=0 would be good. However I’m not aware of any other tool or command working this way (i.e. can be called but due to an env variable would do nothing). Since there are other ways, I’m not sure if it would be good to add yet another way.

I’ll revisit this idea for husky 8 though (which is planned to release when Node 12 reaches end of support).

Thanks for the feedback 👍

npm will execute prepare right after install packages. When you npm ci --production then npm execute prepare

you can checkout here

prepare (since npm@4.0.0)

  • Runs any time before the package is packed, i.e. during npm publish and npm pack
  • Runs BEFORE the package is packed
  • Runs BEFORE the package is published
  • Runs on local npm install without any arguments
  • Run AFTER prepublish, but BEFORE prepublishOnly
  • NOTE: If a package being installed through git contains a prepare script, its dependencies and devDependencies will be installed, and the prepare script will be run, before the package is packaged and installed.
  • As of npm@7 these scripts run in the background. To see the output, run with: --foreground-scripts.

so solution is that you move your husky to optionalDependencies this will ignore when building code on production server.

Thanks bro ❤️

@MikeMcC399 Ahh… I’ll correct the typo then. And yes, npm install -o solve this problem for me. Also I think that’s a logical solution, not a work around one.

Workaround just broke as Node 16 has been promoted to LTS 😞

same issue, i have to use RUN yarn install --ignore-scripts