I was having trouble getting npm install to work when NODE_ENV was set to production, and since I don’t have enough depth in Node to understand what was going wrong, I thought I’d ask why that might be the case.
For context:
-
This project uses node 18.14.2
-
package.jsonincludes a script calledpreparethat simply runs
husky install, and that’s executed as part ofnpm install. -
When
NODE_ENVis set to its default valuedevelopment,npm installto complete successfully:file-upload git:(ACW-5868) ✗ export NODE_ENV=development file-upload git:(ACW-5868) ✗ npm install > @icpsr/file-upload@0.1.29 prepare > husky install husky - Git hooks installed added 1397 packages, and audited 1845 packages in 5s 236 packages are looking for funding run `npm fund` for details 5 moderate severity vulnerabilities To address all issues (including breaking changes), run: npm audit fix --force Run `npm audit` for details. -
However, when it’s set to
production, I get this:file-upload git:(ACW-5868) ✗ export NODE_ENV=production file-upload git:(346b304) npm install > @icpsr/file-upload@0.1.29 prepare > husky install sh: husky: command not found npm ERR! code 127 npm ERR! path /Users/bhorvath/tmp/file-upload npm ERR! command failed npm ERR! command sh -c husky install```
What is going on here? Why is node unable to find an executable that’s within the root directory structure when the build is run in a production environment? (Not that you’d necessarily want to do so btw; I’m just curious.)
>Solution :
You probably ran npm install husky --save-dev to install that package (as the official documentation suggest: https://typicode.github.io/husky/getting-started.html#manual).
--save-dev installs packages used only for development, that’s why you cannot run it in a production environment.
You can check it by opening your package.json file and check if husky is under devDependencies.
To use husky in a production environment, you can refer to the official doc: https://typicode.github.io/husky/guide.html#disable-husky-in-ci-docker-prod