The npm install command is one of the most commonly used commands in Node.js development. It allows developers to install dependencies from the package.json file as well as additional packages from the npm registry. This command simplifies the process of setting up a project managing dependencies and ensuring all necessary modules are available for development.
The npm install command is fundamental to the Node.js projects. It fetches and installs the packages listed in the package.json file or specific packages from the npm registry. Whether we're setting up a new project adding the new libraries or updating existing dependencies npm install is at the heart of the workflow.
The command can be used to:
- Install all project dependencies defined in the package.json.
- Install specific packages.
- Update dependencies or ensure the environment matches the project’s configuration.
- Understanding how npm install works and the various options it supports is crucial for effective project management.
Syntax: The basic syntax of the npm install command is
npm install [<package-name>] [--save-dev] [--global]
- <package-name>: The name of the package we want to the install.
- --save-dev: Installs the package as a development dependency.
- --global (or -g): Installs the package globally so it can be used in the any project.
Default Behavior: If no package name is provided the npm install will install all dependencies listed in the package.json file.
npm install
Key Features of npm install
- Automatic Dependency Installation: When running the npm install npm reads the package.json file and installs the required dependencies automatically.
- Flexible Package Installation: We can install packages locally or globally.
- Version Control: We can specify exact versions or version ranges for the packages to the ensure compatibility and stability across your project.
- Peer and Dev Dependencies: The npm install can handle both the development dependencies and peer dependencies for the managing shared libraries.
- Caching: The npm caches installed packages to the speed up future installations and reduce network load.
Installing Local and Global Packages
There are two types of the package installations in the npm: local and global.
Local Installation:
By default, npm install installs packages locally in the project under the node_modules folder. These packages are only available to the project where they are installed.
For example to install a local package:
npm install lodash
This will install lodash into the project’s node_modules folder and add it to the package.json file as the dependency.
Global Installation
The Global installations are used for the packages that need to be accessible from the anywhere on the system such as the command-line tools. When we install a package globally it can be used across the all projects.
To install a package globally use the --global or -g flag:
npm install -g nodemon
Now, nodemon will be available globally and we can use it in the any project.
Package Management with package.json
The package.json file is central to the managing dependencies in the Node.js project. It contains information about the project its dependencies and other metadata.
Installing All Dependencies
If your project has a package.json file with the listed dependencies running the following the command will install all the dependencies:
npm install
The npm will automatically install all the dependencies and place them in the node_modules folder.
Adding a New Dependency
To add a new dependency and save it to the package.json we can run:
npm install express --save
This will install the express package and automatically add it to the dependencies section in the package.json.
Adding Development Dependencies
The Development dependencies are packages that are only required during the development such as the testing frameworks or build tools. We can install these using the --save-dev flag:
npm install jest --save-dev
This will add jest to the devDependencies section of package.json.
Common Options of npm install
The npm install command supports several useful options to the enhance its functionality:
- --save: The Adds the installed package to the dependencies section in the package.json.
- --save-dev: The Adds the installed package to the devDependencies section used for the development purposes only.
- -g or --global: Installs the package globally making it available system-wide.
- --no-save: The Prevents adding the installed package to the package.json.
- --legacy-peer-deps: Ignores peer dependency warnings and installs the package anyway.
Examples of Using npm install
Let’s look at some examples of the how to use npm install effectively.
Example 1: Installing All Project Dependencies
If we clone a project with the package.json file we can install all required dependencies with:
npm install
Output:
npm install commandExample 2: Installing a Specific Package
To install a specific package like lodash and add it to the dependencies list in the package.json:
npm install lodash
Output:
npm install command
Similar Reads
npm install-test Command
Among the multitude of commands that npm offers, the npm install-test command provides an efficient way to install dependencies and immediately run tests. In this article, we'll explore the npm install-test command in detail, including its configuration options, usage scenarios, and practical benefi
5 min read
npm install-ci-test command
In modern web development, the efficiency of managing dependencies and testing code is crucial. The npm install-ci-test command is a powerful tool designed to streamline the process of installing dependencies and running tests in Continuous Integration (CI) environments. This article provides a deta
3 min read
npm edit command
The command that can simplify the editing process of the project's files is npm edit. This command is useful for opening a file editor directly from the command line which allows users to make changes to the projectâs files effortlessly. This article will provide a comprehensive overview of the npm
3 min read
npm mysql Command
npm MySQL integrates MySQL, a popular open-source relational database, with Node.js applications using Node Package Manager (npm). MySQL is widely used to manage structured data. When combined with Node.js, it allows developers to build dynamic, scalable, and powerful web applications that interact
8 min read
NPM Docs Command
The npm docs command is a convenient way to access the official documentation of any npm package directly from the command line. It opens the documentation page of the specified package in your default web browser, making it easier for developers to quickly find and explore package details, APIs, an
4 min read
CLI Commands in NPM
NPM, short for Node Package Manager, is the default package manager for NodeJS. It is a command-line utility that allows you to install, manage, and share packages or modules of JavaScript code. These packages can range from small utility libraries to large frameworks, and they can be easily integra
4 min read
npm explain Command
The npm explain command is a powerful tool in the Node Package Manager (npm) that allows developers to get a detailed view of how a particular package or dependency fits into their project. In this article, weâll dive into what the npm explain command is, how it works, and practical examples. What I
3 min read
NPM Diff Command
The npm diff command is used to compare changes between different versions of a package, changes in your working directory, or even changes in dependencies. This command helps developers identify modifications made to package files, dependencies, or configurations, allowing for a clear view of what
4 min read
Important npm Commands
Node Package Manager (npm) stands at the core of JavaScript development, serving as a robust package manager for handling dependencies, project initialization, and script execution. Understanding the essential npm commands is important for managing dependencies and automating tasks. In this article,
3 min read
npm doctor command
The npm doctor command is one of the features available in npm with the primary purpose of performing a health check on your npm environment, it evaluates if the general condition of the npm that is being used is in accordance with the general norms. Syntax: npm doctorThese are the following topics
3 min read