The npm install command and how it works with and without arguments. Learn what happens under the hood with dependency resolution and the difference between npm dependencies and devDependencies. (2024)

If you're new to npm, start here...

npm install downloads a package and it's dependencies.

npm install can be run with or without arguments.

When run without arguments, npm install downloads dependencies defined in a package.json file and generates a node_modules folder with the installed modules.

When run with arguments, npm install downloads specific modules to the node_modules folder.

The package.json file dictates what modules will get installed in the node_modules folder. It's important that npm install is run in the same location as the package.json file.

Let's look at some examples in more detail...

npm install (no arguments)

npm installnpm i

Both of these commands do the same thing. Running npm install without arguments installs modules defined in the dependencies section of the package.json file.

It's important that npm install is run in the same directory as the package.json file.

The downloaded modules are placed in a node_modules folder in the same location as package.json.

npm install also generates a package-lock.json file. This file describes the exact dependency tree that was installed. This makes it easier for subsequent installs to use the exact same tree (regardless of intermediate dependency updates).

npm dependency resolution

What's this tree you ask? Remember that npm install downloads a package and any dependencies it relies on. Sometimes different packages require different versions of the same module.

This can lead to a situation where module A requires v1 of module C and module B requires v2 of module C. In this case, both versions of the same module C are installed in a tree like structure.

The order in which the dependencies are installed dictates this tree structure. For example, v1 of module C may get installed at the root level while v2 is installed as a nested dependency of module B.

The details behind how npm resolves dependencies is outside the scope of this article, but the package-lock.json file can help insure the exact tree is installed with subsequent installs.

npm install (with arguments)

npm install can be run with different arguments. Here are some of the more important ones to be aware of...

npm install (specified package)

npm install express

When npm install is run with a specified package argument, it installs the package in the existing node_modules directory.

You can optionally provide a specific version as well...

npm install [email protected]

When a version is not provided, npm automatically downloads the latest stable version.You can also specify several packages in the same command...

npm install express eslint mongo

This will install express, eslint, and mongo in a single command.

npm install (directory)

npm install myfolder

When a folder argument is specified, npm install downloads everything from the specified location to the original node_modules directory at the root of the project.

This assumes a myfolder sub-directory exists in the same location as package.json. Additionally, the myfolder has it's own package.json

npm install WILL NOT generate a new node_modules folder inside the sub-directory. Instead, the dependencies are hoisted to the root directory's node_modules folder.

npm install (from git)

npm install (with options)

In addition to arguments, npm install can be run with different options. Here are some of the more important ones to be aware of...

npm install (with --global)

npm install -gnpm install --global

When run with --global or -g, npm install installs the package globally. This means the package is installed in two places. The first is at the root directory where package.json is defined. The second is the global node_modules folder on the user system.

As a general rule, you want to install packages globally when they will be reused across projects or when they provide executable commands from the CLI.

npm install (with --save)

npm install express --save

When run with --save, npm install modifies the package.json file to include the specified package as a dependency. In this case, the express package will be added as a dependency to package.json.

The --save option is important whenever you want future installs to include the specified package.

npm install (with --save-dev)

The --save-dev flag specifies that the package should be added to the devDependencies section of the package.json rather than the dependencies section.

npm dependencies vs devDependencies

So what's the difference? Packages included as devDependencies won't get installed when the optional --production flag is used. This makes it possible to exclude packages you only need for development.

For example, linters are popular for enforcing clean code but aren't necessary in production. You would include a linter package as a devDependency so you can run linters against your code locally without including it in a production build.

npm install (with --production)

npm install -pnpm install --production

The --production flag specifies to exclude devDependencies from the install. This means any dependency listed under the devDependencies section of package.json won't get installed when this flag is present.

The npm install command and how it works with and without arguments.  Learn what happens under the hood with dependency resolution and the difference between npm dependencies and devDependencies. (2024)

FAQs

What is the difference between dev dependencies and devDependencies? ›

Dependencies are the crucial packages required for your application to function in a production environment. At the same time, devDependencies are tools and utilities that aid in the development process but are not needed in production.

Does npm install install devDependencies? ›

NPM installs devDependencies within the package. json file. The 'npm install' command should add all the dependencies and devDependencies automatically during installation. If you need to add specific devDependencies to your project, you can use this command- 'npm install --save-dev'.

What does the npm install command do? ›

This command installs a package and any packages that it depends on. If the package has a package-lock, or an npm shrinkwrap file, or a yarn lock file, the installation of dependencies will be driven by that, respecting the following order of precedence: npm-shrinkwrap. json.

What is the difference between peerDependencies and dependencies in npm? ›

Dependencies are the packages that are required for your application to run properly, devDependencies are the packages that are required for development and testing purposes only, and peerDependencies are the packages that your package expects to be installed in the user's environment.

What is the difference between npm install and npm install -- Save Dev? ›

You obviously don't want to bundle it as part of your final scripts. Npm install package installs the package and add in dependency section of your package. json file whereas npm install --save-dev does as well install the package but add it in dev-dependencies section of your package. json file.

When you run npm install in a node project, how are the dependencies used? ›

npm install will read the package. json file and download all the specified dependencies. It will create a folder named “node_modules” in your project directory, where it stores all the installed packages. Once the installation process is complete, you can start utilizing the installed dependencies in your code.

How does npm install packages? ›

To install a package, npm uses the following algorithm:
  1. load the existing node_modules tree from disk.
  2. clone the tree.
  3. fetch the package.json and assorted metadata and add it to the clone.
  4. walk the clone and add any missing dependencies.
  5. dependencies will be added as close to the top as is possible.
Sep 22, 2020

Does npm automatically install peer dependencies? ›

When you use peer dependencies, npm will not automatically install those dependencies (see comments above in respect to npm version 7). This can lead to errors when you are running tests on your package, although you will get warnings when executing npm to prompt you to install the peer dependencies.

Does npm install update dependencies? ›

npm-check-updates upgrades your package. json dependencies to the latest versions, ignoring specified versions. If you have the dependencies installed already, you can delete the node_modules folder from your project and then run npm install and all dependencies will be installed to the latest version.

What happens when npm install is run? ›

The above command will install the express module into /node_modules in the current directory. Whenever you install a module from npm, it will be installed into the node_modules folder. Once you run this, npm will begin the installation process of all of the current project's dependencies.

What is the difference between npm I and npm install? ›

npm i: The npm i (or npm install) is used to install all dependencies or devDependencies from a package. json file. npm ci: CI stands for clean install and npm ci is used to install all exact version dependencies or devDependencies from a package-lock. json file.

What does npm install serve? ›

Serve is used for quickly setting up local servers to host static web projects that are in development. Basically, it turns the current working directory into a virtual server where you can open HTML, CSS, JavaScript files and other static assets in a browser.

Does npm install install dev dependencies? ›

If you run npm install on a cloned repository, npm assumes that you are developing the project. That's why it will also install all the dependencies listed in the devDependencies object. The package manager will only install the dependencies listed in the dependencies object.

What is the difference between dependencies and devDependencies? ›

A dependency is a library that a project needs to function effectively. DevDependencies are the packages a developer needs during development. A peer dependency specifies that our package is compatible with a particular version of an npm package.

How to resolve peer dependencies in npm? ›

To resolve conflicting peer dependencies, developers can:
  1. Manually adjust the package. ...
  2. Use the npm install command with the -force or -legacy-peer-deps flags to bypass the conflict.
  3. Update all related packages to the latest versions that have compatible peer dependencies.
Mar 13, 2024

What is an example of a dev dependency? ›

Dev dependencies are packages required only for the development phase and not for the application to run in production. Some common examples of such packages are testing frameworks (jest, cypress, karma, mocha), code linters (prettier, eslint), build tools (babel, webpack), and more.

What are dependencies in software dev? ›

What is a software dependency? A software dependency is a relationship between software components where one component relies on the other to work properly. For example, if a software application uses a library to query a database, the application depends on that library.

What is the difference between embed and dependencies? ›

Technical Background. Client libraries can have dependencies and embeds. The difference between those two concepts is that dependencies lead to additional includes/requests, while embeds are contained in the original client library's response (i.e. are concatenated with the JS/CSS of the client library itself).

What is the difference between plugins and dependencies in POM XML? ›

Plugins are software components that extend the functionality of another software application. Dependencies are external libraries or modules used to build and run a software project.

Top Articles
If you have forgotten your Apple ID password
Tutorial: High Availability for API Gateways in Multi-Cloud and Hybrid Environments - NGINX
Calvert Er Wait Time
Knoxville Tennessee White Pages
فیلم رهگیر دوبله فارسی بدون سانسور نماشا
Professor Qwertyson
Legacy First National Bank
Craigslist Phoenix Cars By Owner Only
Heska Ulite
Espn Expert Picks Week 2
Youtube Combe
How To Delete Bravodate Account
Shooting Games Multiplayer Unblocked
Flower Mound Clavicle Trauma
Hartland Liquidation Oconomowoc
Nyuonsite
Vanessa West Tripod Jeffrey Dahmer
Classic | Cyclone RakeAmerica's #1 Lawn and Leaf Vacuum
1773X To
Army Oubs
Yard Goats Score
Integer Division Matlab
Chime Ssi Payment 2023
Southwest Flight 238
The 15 Best Sites to Watch Movies for Free (Legally!)
JVID Rina sauce set1
Log in to your MyChart account
Marlene2295
Mercedes W204 Belt Diagram
Evil Dead Rise - Everything You Need To Know
1475 Akron Way Forney Tx 75126
Ilabs Ucsf
Strange World Showtimes Near Regal Edwards West Covina
The Ride | Rotten Tomatoes
Ippa 番号
The Land Book 9 Release Date 2023
Tal 3L Zeus Replacement Lid
Babylon 2022 Showtimes Near Cinemark Downey And Xd
Viewfinder Mangabuddy
Fototour verlassener Fliegerhorst Schönwald [Lost Place Brandenburg]
Man Stuff Idaho
Tgirls Philly
Owa Hilton Email
Chase Bank Zip Code
Professors Helpers Abbreviation
Random Animal Hybrid Generator Wheel
Victoria Vesce Playboy
Booknet.com Contract Marriage 2
17 of the best things to do in Bozeman, Montana
Acuity Eye Group - La Quinta Photos
Spongebob Meme Pic
Rise Meadville Reviews
Latest Posts
Article information

Author: Wyatt Volkman LLD

Last Updated:

Views: 5888

Rating: 4.6 / 5 (46 voted)

Reviews: 93% of readers found this page helpful

Author information

Name: Wyatt Volkman LLD

Birthday: 1992-02-16

Address: Suite 851 78549 Lubowitz Well, Wardside, TX 98080-8615

Phone: +67618977178100

Job: Manufacturing Director

Hobby: Running, Mountaineering, Inline skating, Writing, Baton twirling, Computer programming, Stone skipping

Introduction: My name is Wyatt Volkman LLD, I am a handsome, rich, comfortable, lively, zealous, graceful, gifted person who loves writing and wants to share my knowledge and understanding with you.