How to install/uninstall an npm package

in Code Write a comment

Learn the basics of installing and uninstalling npm packages, locally and globally.

How to install an npm package

First of all, let’s learn how to install an npm package/module. It can be done by running this simple command:

npm install <package_name>

Shorthand for npm install <package_name>

npm i <package_name>

When you install a package locally it will install it to the local node_modules folder.

Install an npm package and save it as a dependency

npm install --save <package_name>

Shorthand for npm install --save <package_name>

npm i -S <package_name>

When you install a package as a dependency it will add it to package.json file as a dependency.

Install an npm package and save it as a dev dependency

npm install --save-dev <package_name>

Shorthand for npm install --save-dev <package_name>

npm i -D <package_name>

When you install a package as a dev dependency it will add it to package.json file as a devDependency.

Installing a package globally

Node.js packages can be installed and uninstalled globally or locally by appending -g to npm command.

For example, if you want to install an npm package globally run this command:

npm install --global <package_name>

Shorthand for npm install --global <package_name>

npm i -g <package_name>

When you install a package globally (in global mode -g or --global) it will install a package that can be accessed globally.

Uninstalling npm packages

Similarly to installing npm packages they can be uninstalled (locally and globally).

To uninstall an npm package run the following command.

npm uninstall <package_name>

To uninstall a package globally

npm uninstall -g <package_name>

Uninstalling npm package from dependencies

If the package was listed in the dependences of the package.json, you need to run this command to remove it from the file.

npm uninstall -S <package_name>

Uninstalling npm package from devDependencies

If the package was listed in the development dependences of the package.json, you need to run this command to remove it from the file.

npm uninstall -D <package_name>

Install packages using package.json

If your project contains a package.json file you can install all modules listed there by running this command:

npm install

Shorthand for npm install

npm i

It will install dependencies from the package.json file in the local node_modules folder.

If you find this post useful, please let me know in the comments below and subscribe to my newsletter. 
Cheers,
Renat Galyamov

Want to share this with your friends?
👉renatello.com/install-uninstall-npm-package

PS: Make sure you check other Node.js tutorials.

Write a Comment

Comment