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
- Get Help
- Create package.json
- Set Defaults
- Get Defaults
- Remove Defaults
- Install Packages
- Install Certain Version
- Update Packages
- Remove Packages
- Move to Another Folder
- Find Root Folder
- List Packages
- NPM Scripts
- Package Version
Get Version
npm -v
npm --version
Get Help
npm
npm help
Create package.json
npm init
# Below commands will create package.json file with default values
npm init -y
npm init --yes
Set Defaults
npm config set init-author-name 'YOUR NAME'
npm config set init-license 'MIT'
Get Defaults
npm config get init-author-name
npm config get init-license
Remove Defaults
npm config delete init-author-name
npm config delete init-license
Install Packages
Globally
npm install -g package-name
Production dependency
npm install --save package-name
Development dependency
npm install --save-dev package-name
Install Certain Version
Globally
npm install -g package-name@package-version
Production dependency
npm install --save package-name@package-version
Development dependency
npm install --save-dev package-name@package-version
Update Packages
Globally
npm update -g package-name
Production dependency
npm update --save package-name
Development dependency
npm update --save-dev package-name
Remove Packages
Globally
npm uninstall -g package-name
Production dependency
npm uninstall --save package-name
Development dependency
npm uninstall --save-dev package-name
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
Install production dependencies only.
npm install --production
Find Root Folder
Globally
npm root -g
Locally
npm root
List Packages
Globally
npm list -g
npm list -g --depth 0
npm list -g --depth 1
Locally
npm list
npm list --depth 0
npm list --depth 1
NPM Scripts
Define scripts in package.json
file.
"scripts": {
"start": "node index.js",
"script-name":"command-to-run"
}
Run scripts
# Only "start" script will execute without run command
npm start
npm run script-name
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"
Top comments (1)
It's now recommended to use --omit=dev over --production