DEV Community

Cover image for NPM Cheat Sheet
Ganesh Shinde
Ganesh Shinde

Posted on

NPM Cheat Sheet

This cheat sheet includes commands which will help you while using NPM packages and modules.
Don't worry about remembering all these NPM commands, Take a look at this cheat sheet whenever required.😉
Now go ahead and create something awesome.😎

Table of Contents

Get Version

npm -v

npm --version
Enter fullscreen mode Exit fullscreen mode

Get Help

npm

npm help
Enter fullscreen mode Exit fullscreen mode

Create package.json

npm init

# Below commands will create package.json file with default values
npm init -y

npm init --yes
Enter fullscreen mode Exit fullscreen mode

Set Defaults

npm config set init-author-name 'YOUR NAME'

npm config set init-license 'MIT'
Enter fullscreen mode Exit fullscreen mode

Get Defaults

npm config get init-author-name

npm config get init-license
Enter fullscreen mode Exit fullscreen mode

Remove Defaults

npm config delete init-author-name

npm config delete init-license
Enter fullscreen mode Exit fullscreen mode

Install Packages

Globally

npm install -g package-name
Enter fullscreen mode Exit fullscreen mode

Production dependency

npm install --save package-name
Enter fullscreen mode Exit fullscreen mode

Development dependency

npm install --save-dev package-name
Enter fullscreen mode Exit fullscreen mode

Install Certain Version

Globally

npm install -g package-name@package-version
Enter fullscreen mode Exit fullscreen mode

Production dependency

npm install --save package-name@package-version
Enter fullscreen mode Exit fullscreen mode

Development dependency

npm install --save-dev package-name@package-version
Enter fullscreen mode Exit fullscreen mode

Update Packages

Globally

npm update -g package-name
Enter fullscreen mode Exit fullscreen mode

Production dependency

npm update --save package-name
Enter fullscreen mode Exit fullscreen mode

Development dependency

npm update --save-dev package-name
Enter fullscreen mode Exit fullscreen mode

Remove Packages

Globally

npm uninstall -g package-name
Enter fullscreen mode Exit fullscreen mode

Production dependency

npm uninstall --save package-name
Enter fullscreen mode Exit fullscreen mode

Development dependency

npm uninstall --save-dev package-name
Enter fullscreen mode Exit fullscreen mode

Move to Another Folder

NPM stores installed packages inside node_modules folder.
Most people usually doesn't share node_modules folder along with their code, because you can easily install all the NPM packages they have used using below commands.

Install production and development dependencies.

npm install
Enter fullscreen mode Exit fullscreen mode

Install production dependencies only.

npm install --production
Enter fullscreen mode Exit fullscreen mode

Find Root Folder

Globally

npm root -g
Enter fullscreen mode Exit fullscreen mode

Locally

npm root
Enter fullscreen mode Exit fullscreen mode

List Packages

Globally

npm list -g

npm list -g --depth 0

npm list -g --depth 1
Enter fullscreen mode Exit fullscreen mode

Locally

npm list

npm list --depth 0

npm list --depth 1
Enter fullscreen mode Exit fullscreen mode

NPM Scripts

Define scripts in package.json file.

"scripts": {
  "start": "node index.js",
  "script-name":"command-to-run"
}
Enter fullscreen mode Exit fullscreen mode

Run scripts

# Only "start" script will execute without run command
npm start

npm run script-name
Enter fullscreen mode Exit fullscreen mode

Package Version

Find below, What package version with different symbols (*, ~, ^) represents in package.json file.

Version Result
"*" Install package with latest version
"4.17.3" Install package with exact version
"~4.17.3" Install package with latest patch update (Highlighted part gets updated)
"^4.17.3" Install package with latest minor update (Highlighted part gets updated)

Most Preferred

"package-name": "^4.17.3"
Enter fullscreen mode Exit fullscreen mode

Top comments (0)