Installing Production-Only Dependencies with Dev Dependency Hooks in Node.js
Emmanuel Gautier / October 12, 2023
2 min read
There might be situations where you want to install production dependencies only but you have npm scripts using a dev dependency command. For example it can be the case if you use the husky
package. In this blog post, we'll look how to install production-only dependencies with hooks using dev dependency.
The Problem
One common problem developers face is that the commands from dev dependencies are not available when installing production dependencies. This is because npm install only production dependencies and the command used in the script uses a command from a dev dependency not installed. This can result in a "command not found" error when you attempt to use a command from a dev dependency in your project.
The Solution
To overcome this issue and ensure that hooks will not be blocking when installing production dependencies, you can disable the prepare npm hook. Depending on the version of npm you are using, there are different solutions to achieve this:
NPM 7 and below
Run the following command to disable the prepare
hook for all dependencies:
npm set-script prepare ""
npm ci --omit=dev
This command sets an empty script for the prepare hook, effectively disabling it for all dependencies.
NPM 8 and above:
Run the following command to remove the prepare
hook from the scripts
section of your package.json
file:
npm pkg delete scripts.prepare
npm ci --omit=dev
This command will remove the scripts.prepare
key from your package.json
file, ensuring that the prepare
hook is no longer executed during the installation of dependencies.
Consulting
If you're seeking solutions to a problem or need expert advice, I'm here to help! Don't hesitate to book a call with me for a consulting session. Let's discuss your situation and find the best solution together.
Featured Posts
Introducing new blog about OAuth, OpenID Connect, and IAM Solutions
I'm excited to announce the launch of a new blog named CerberAuth, where I'll be exploring the world of OAuth, OpenID Connect, and IAM solutions for modern security.
How to deal with Docker Hub rate limit on AWS
Since 2020, DockerHub has been limited to only 200 container image pull requests per six hours. This article will help you to deal with this limitation on AWS.
How to enable Python type checking in VSCode
Python now has support for type hints. In this article, we will see how to enable better IntelliSense and type checking analysis in VSCode.