NPM stands for Node Package Manager and it is a package manager for JavaScript committed to making JavaScript development elegant, productive, and safe. Here I have shared some useful npm commands.
Get Version
npm --version
// shorthand
npm -v
Create Package.json
npm init
// Below commands will create package.json file with default values
npm init -y
npm init --yes
Install Packages
Global Package
npm install -g package-name
Production Package (default)
npm install --save package-name
Development Package
npm install --save-dev package-name
Install Specific Version
Global Package
npm install -g package-name@package-version
Production Package(default)
npm install --save package-name@package-version
Development Package
npm install --save-dev package-name@package-version
Remove Packages
Global Package
npm uninstall -g package-name
Production Package(default)
npm uninstall --save package-name
Development Package
npm uninstall --save-dev package-name
Clone another repo
When clone any repo, that time node_modules folder don’t get copy on local machine. So in that case to download all packages -use npm i
Install production and dev dependencies both
npm install
or
npm i
Install production dependencies only
npm install --production
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
only “start” script will execute without run command
npm start
npm run <script-name>
npm run build
npm run test
Package Versions
What package version with different symbols (*,~, ^) represents in package.json file
Version | Meaning |
“*” | Install package with latest version |
“16.8.0” | Install package with exact version |
“~16.8.0” | Install package with latest patch update (Highlighted part gets updated) |
“^16.8.0” | Install package with latest minor update (Highlighted part gets updated) |
Note: “^16.8.0” is the most preferred version.
Also Read: