Basic NPM Commands

Sunday, Aug 13, 2023

Introduction

These are the most used NPM commands I find myself using daily. For more references and commands, you can check out the NPM Docs

Initializing a new package.json file

List all NPM packages in a project

List Global NPM Packages

NPM Package Info

Install NPM Package Globally

Install NPM Package for a Specific Project

Uninstall NPM Package

Install NPM Package as a Development Dependency Only

The —save flag is no more used

0- Initializing a new package.json file

:Create a package.json file.

 npm init  

Note: execute this code in the project root folder

Note: You can use the -y flag to accept all the defaults

1- List of NPM Packages From a Selected Project

: Gets a list of all the installed NPM packages in the specific project.

 npm list --depth=0    

Note: execute this code in the project root folder

2- Global NPM Packages List

: Gets a list of all the globally installed NPM packages in your system.

npm list -g

3- NPM Package Info

: Gets a specific package info.

npm info  package-name

: Gets a specific package version info.

npm info  package-name  version

Note: replace the package-name with the real package-name

Note: replace the package-name with the real package-name

version is an (optional flag to get only the selected package version info) --> You can specify a version like the example below ⬇️

Example: npm info react 18.0.0

"If the selected version of the package is not installed the command will return no info, Works only when the package is already installed in a specific project or globally"

4- Install NPM Package Globally

: will install the package globally.

npm install -g package name

Note: replace the package-name with the real package-name

5- Install NPM Package for a Specific Project

: will install the package in the selected project.

npm i  package name

Note: execute this code in the project root folder

Note: replace the package-name with the real package-name

6- Uninstall NPM Package

: Will uninstall the package

npm uninstall  package name

Note: replace the package-name with the real package-name

7- Install NPM Package as a Development Dependency Only

: Save the package under devDependencies ( development-only packages )

npm install  package name  -D

Note: replace the package-name with the real package-name

Example: npm install sass -dev

8- The --save Flag is no More Used.

: The --save flag is not needed any more for installing packages.

If you are watching tutorials or reading articles where they are using or mentioning the --save flag, you don't need to use this anymore.

⚠️ You don't need the --save flag when installing a package, after NPM version 5 as it adds the package to the package.json file automatically.

Quote from the NPM Docs:
As of npm 5.0.0, installed modules are added as a dependency by default, so the --save option is no longer needed .