How to install multiple npm packages with one command

avatar
Borislav Hadzhiev

Last updated: Feb 26, 2023
3 min

banner

# Table of Contents

  1. Install multiple npm packages with one command
  2. Install multiple development npm packages with one command
  3. Install multiple global npm packages with one command
  4. Install multiple npm packages using package.json
  5. Uninstall multiple npm packages with one command
  6. Uninstall multiple development npm packages with one command
  7. Uninstall multiple global npm packages with one command
  8. Uninstall multiple npm packages using package.json

# Install multiple npm packages with one command

Pass multiple, space-separated package names to the npm install method to install multiple npm packages, e.g. npm install package1 package2.

The npm install command will install all of the packages specified in the command in a single go.

Make sure you have a package.json file to be able to run the npm install command.

You can generate a package.json file with the npm init -y command.

shell
npm init -y

You can install multiple packages, by specifying multiple, space-separated names when issuing npm install.

shell
npm install axios express moment

npm install multiple packages

The command in the example installs the axios, express and moment packages in a single go.

The syntax to install multiple packages is npm install package1 package2, where the names of the packages are separated by spaces.

You can also specify the versions of the packages.

shell
npm install axios@1.3.4 express@4.12.3

install multiple packages specify version

The installed packages are added to the dependencies section of your package.json file.

package.json
{ "dependencies": { "axios": "^1.3.4", "express": "^4.12.3" } }

# Install multiple npm packages as development dependencies with one command

If you need to install multiple npm packages as development dependencies with one command, use the --save-dev flag.

shell
npm install --save-dev nodemon jest

install multiple development packages

The command installs the nodemon and jest packages as development dependencies.

The packages are added to the devDependencies object in your package.json file.

package.json
{ "devDependencies": { "jest": "^27.5.1", "nodemon": "^2.0.20" } }

# Install multiple global npm packages with one command

Use the -g flag to install multiple global npm packages with one command.

shell
npm install -g create-react-app rimraf

npm install multiple global packages

The command installs the create-react-app and rimraf packages globally in a single go.

The packages are installed globally, so they don't get added to your package.json file.

You can view the globally installed packages by running the npm list -g command.

shell
npm list -g

view globally installed packages

# Install multiple npm packages using package.json

An alternative approach to installing multiple packages is to specify the names in your package.json file and issue the npm install command.

If you need to initialize a new package.json file, use the npm init -y command.

shell
npm init -y

Here is an example package.json file that installs multiple packages as dependencies and development dependencies.

package.json
{ "dependencies": { "axios": "^1.3.4", "express": "^4.12.3", "moment": "^2.29.4", }, "devDependencies": { "jest": "^29.4.3", "nodemon": "^2.0.20" } }

install multiple packages using package json

You can add the dependencies and devDependencies sections to a package.json file and install the packages using the npm install command.

shell
npm install

install multiple packages from package json

When you issue the npm install command without specifying a package name, all the packages in your package.json file get installed.

# Uninstall multiple npm packages with one command

You can supply multiple space-separated package names to the npm uninstall command to uninstall multiple npm packages with one command.

shell
npm uninstall axios express moment

uninstall multiple packages one command

The command uninstalls the axios, express and moment packages and removes them from the dependencies section of your package.json file.

# Uninstall multiple development npm packages with one command

If you need to uninstall multiple development dependency packages with one command, set the --save-dev flag.

shell
npm uninstall --save-dev nodemon jest

uninstall multiple development dependency npm packages

The command uninstalls the nodemon and jest npm packages and removes them from the devDependencies section of your package.json file.

# Uninstall multiple global npm packages with one command

You can set the -g flag to uninstall multiple globally installed npm packages with one command.

shell
npm uninstall -g create-react-app rimraf

uninstall multiple global npm packages

# Uninstall multiple npm packages using package.json

An alternative approach used to uninstall multiple npm packages with one command is to remove the packages from your package.json file.

You can remove the packages you want to uninstall from the dependencies or devDependencies sections of your package.json file.

Once the packages you want to uninstall are removed, issue the npm install command.

shell
npm install

The npm install command will uninstall the removed packages to sync your node_modules folder and package.json file.

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.