Preventing the Installation of ‘devDependencies’ NPM Modules in Node.js

In this article, we are going to find a way to prevent the installation of “devDependencies” NPM modules for Node.js (package.json). But before diving deep into it let’s discuss a short introduction to the basics of NPM and package.json.

Introduction to NPM

Node.js is a popular JavaScript runtime that has become essential in web development. To manage the many libraries and packages needed in Node.js applications, developers turn to NPM, the Node Package Manager.

NPM is a package manager for JavaScript, primarily used with Node.js. It allows developers to easily install, manage, and share reusable code packages and libraries. These packages can be small utility functions or entire frameworks, and they play a crucial role in simplifying the development process.

Key Features of NPM

Below are some key features of NPM.

  1. Package Installation: NPM simplifies the process of installing packages in your project. With a simple command like npm install package-name, you can add any package from the npm registry.
  2. Dependency Management: NPM automatically manages package dependencies. It keeps track of which packages your project relies on, and it installs them as needed.
  3. Version Control: Developers can specify the version of a package they want to use. This ensures consistency and stability in a project by preventing unexpected updates.
  4. Publishing: NPM also allows developers to publish their own packages.
  5. Scripts: NPM includes a script system that lets you define custom tasks in your project’s package.json file.

So NPM nowadays is a fundamental tool in modern web development. It simplifies package management and helps developers focus on building great software.

For More: NodeJS NPM – A Beginner’s Guide to Node Package Manager

Introduction to package.json

In the world of JavaScript development, the package.json file is like a blueprint for your project. It’s a file that helps you organize and manage your project by keeping track of important information & packages.

The package.json is a file that contains important information about your project, like its name, version, and what other tools it needs to work correctly. It’s used to make sure everyone working on the project uses the same tools, settings & libraries.

Key Features of package.json

Below are some key features of package.json.

  • Project Information: package.json has details about your project, such as its name, version, and who’s working on it.
  • Dependencies: It lists the tools and libraries your project relies on. This helps you and your team easily install and update them.
  • Dev Dependencies: These are the tools needed only during development, like testing or building the project.
  • Scripts: You can create shortcuts for common tasks, like running your project, in the scripts section.
  • License: This shows what rules your project follows, making sure it’s legal and clear for others to use.

So to sum it up, package.json is a critical part of JavaScript development. It acts as a guide for your project.

For More: Updating Dependencies in package.json

Now let’s understand about dependencies especially dev dependencies which is critical for this article.

Dev Dependencies in package.json

Dev dependencies in your package.json are special tools and libraries for building, testing, and maintaining your project during development. Dev dependencies help you and your team work more efficiently and maintain a high-quality project. In your package.json file, regular dependencies ensure your project functions correctly, while dev dependencies are the secret helpers that make your development process smoother and more productive. Remember, dev dependencies are not included when others use your project; they’re just for your development environment.

Example:

Below is an example of package.json with devDependencies.

{
  "name": "your-project",
  "version": "1.0.0",
  "dependencies": {
    "regular-dependency": "^1.0.0"
  },
  "devDependencies": {
    "dev-dependency": "^2.0.0"
  }
}

How to prevent the installation of “devDependencies”?

To prevent the installation of “devDependencies” when running npm install in a Node.js project, you can use the –-only=prod flag or the –production flag. This tells npm to only install the production dependencies listed in the “dependencies” section of your package.json file and not in the “devDependencies”.

Let us look at both of these one by one.

Using the ‘–only=prod’ flag when running ‘npm install’

Run the following command in your project directory:

npm install --only=prod

This command will only install the dependencies listed under the “dependencies” section of your package.json and skip the “devDependencies”.

Using the ‘–production’ flag when running ‘npm install’

Alternatively, you can use the –production flag, which is equivalent to –only=prod.

npm install --production

This command will also install only the “dependencies” and skip the “devDependencies”.

After running either of these commands, only the production dependencies will be installed, which can be useful in scenarios where you want to save space or are deploying your application to a production environment. If you later want to install the “devDependencies”, you can simply run npm install without any additional flags.

Conclusion

Now we have reached to the end of this article. Hope you understood “How do you prevent install of devDependencies NPM modules for Node.js (package.json)”. Effective dependency management is crucial in Node.js projects. The “dependencies” and “devDependencies” sections in the package.json file play a pivotal role in this. By using flags like –only=prod or –production during the npm install command, developers can control the installation of only essential production dependencies, omitting those designated for development and testing. For more such articles on Node and Express follow https://codeforgeek.com/.

References

Arnab Biswas
Arnab Biswas
Articles: 10