Nodejs npm command list | npm checklist example
This tutorial list out all frequently used npm commands in a nodejs application. You can also check other posts on npm command deprecate option is deprecated
Nodejs Npm commands list
Below are command list for use with nodejs applications
How to find the npm version
npm version command list out all versions of nodejs project and dependent tools versions.
B:\Workspace\fullstackapp\backend-nodejs>npm version
{
'backend-nodejs': '1.0.0',
npm: '8.12.0',
node: '14.17.0',
v8: '8.4.371.23-node.63',
uv: '1.41.0',
zlib: '1.2.11',
brotli: '1.0.9',
ares: '1.17.1',
modules: '83',
nghttp2: '1.42.0',
napi: '8',
llhttp: '2.1.3',
openssl: '1.1.1k',
cldr: '38.1',
icu: '68.2',
tz: '2020d',
unicode: '13.0'
}
To know only the npm version
B:\Workspace\fullstackapp\backend-nodejs>npm --v
8.12.0
How to create aCreate a nodejs project
Create a project folder and run the below command to create and initialize a nodejs project. It stores all the
npm init
It asks prompt to add details to enter by the user as given below
- package name
- Project version
- Description
- License
- Entry point of a project
- Author details.
This command creates a package.json
file in the project folder
To avoid the prompt, run the below command to use default values.
npm init -y
It creates a new project folder with package.json that contains default properties.
How to install and uninstall dependencies in nodejs application
- To install all dependencies defined in package.json
npm install
This command does the following things
Download package.json direct and indirect dependencies into node_modules of a current project.
Create and update the package-lock.json file
To install a single package dependency
npm install package
The package name is the name of the node package
For example, Run the below command to install lodash
npm install lodash
- To uninstall single or all packages
Below uninstalls all packages
npm uninstall
It uninstalls a single package
npm install lodash
Install packages globally in node module
option -g
or --global
uses to install the node packages globally.
npm install -g @angular/cli
or
npm install --global @angular/cli
This command does the following things
- Download package dependency into node_modules of a current user npm global node_modules location, here the location is specific to OS. In WIndows, The locaiton is
%USERPROFILE%\AppData\Roaming\npm\node_modules
- Create and update the package-lock.json file in the user’s global location
- To uninstall global packages
npm uninstall -g @angular/cli
or
npm uninstall --global @angular/cli
How to install dev packages in node application
option -d
or --save-dev
uses to install the node packages as a devDependencies in package.json. These packages are useful during development and it has no dependency on a production build.
This is mainly used for tools and frameworks to develop and test cases for a node application
npm install --save-dev @angular/cli
This adds below content to package.json file
"name": "myapp",
"version": "1.0.0",
"dependencies": {
"my_dep": "^1.0.0",
"another_dep": "~2.2.0"
},
"devDependencies" : {
"angular/cli": "^12.1.0",
}
This command does the following things
Download package dependency into node_modules of a current project node_modules location.
Create and update the package-lock.json file in the current project
To uninstall dev packages
or
npm uninstall --save-dev @angular/cli
Npm local global location
npm root command lists the location based on global, user, or project level
npm root —location=global/user/project
global
points global npm location that installs dependencies to node_modules.
C:\Users\abc>npm root --location=global
C:\Users\abc\AppData\Roaming\npm\node_modules
user
: user npm location
C:\Users\abc>npm root --location=user
C:\Users\abc\node_modules
project
: location of the current project
B:\Workspace\backend-nodejs>npm root --location=project
B:\Workspace\backend-nodejs\node_modules
Npm update command list
packages can be updated using the update command. It basically changes the version of the package to the latest
- upgrade all packages to the latest.
npm update
- To update a single package
npm update package
- To upgrade global packages
npm update --global
Npm installed package list commands
- list all packages in a project
npm list
check and show outdated packages
npm outdated
Clean packages
The below command removes the local npm package cache
npm cache clean
npm script commands for execution
Below commands used to execute different scripts based on project
- To Start development web server
npm run start
or
npm start
- To build application for production
npm run build
or
npm build
- To execute unit and other test cases
npm run test
or
npm test