NPM Cheat Sheet
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.
npm -v
npm --version
npm
npm help
npm init
# Below commands will create package.json file with default values
npm init -y
npm init --yes
npm config set init-author-name 'YOUR NAME'
npm config set init-license 'MIT'
npm config get init-author-name
npm config get init-license
npm config delete init-author-name
npm config delete init-license
Globally
npm install -g package-name
Production dependency
npm install --save package-name
Development dependency
npm install --save-dev package-name
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
Globally
npm update -g package-name
Production dependency
npm update --save package-name
Development dependency
npm update --save-dev package-name
Globally
npm uninstall -g package-name
Production dependency
npm uninstall --save package-name
Development dependency
npm uninstall --save-dev package-name
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
Globally
npm root -g
Locally
npm root
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
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
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"