husky: command not found with npm install --production
Recently, while attempting to build a Docker container, I ran into a bit of a
dilemma. Upon running npm install --production I was greeted with this less
than ideal error message:
> @company/project-api@1.0.0 prepare
> husky install
sh: husky: command not found
Well of course husky isn't installed, as it's listed in devDependencies and
we're attempting to install the minimum amount of dependencies by way of the
--production (or --omit=dev) argument.
The issue is that regardless of which dependencies we've installed, the
prepare script in package.json is still attempting to run. With husky
unavailable, we get an error.
After some research, this does appear to be a known issue with husky that may
or may not be resolved at some point in the future.
As this is all happening inside of a Dockerfile, I ventured to solve this in
the Dockerfile used to build the image. This approach, instead of munging
with things in package.json, means that any changes are isolated away from
an engineer's interaction with the code locally.
It seemed like the safer bet.
Since our package.json only has husky install in the prepare script, all
we needed to do was clear said script out in our Dockerfile with the following
command and then proceed with our npm install:
npm set-script prepare ''
To put it all together inside of the Dockerfile, it looked something like
this:
RUN npm set-script prepare '' && npm install --production
With the prepare script set to an empty string, there's no longer anything to
error out and the npm install proceeds as expected!