NPM is the package manager for Node.js, used to install, share, and manage dependencies of Node.js and JavaScript projects.
NPM Installation
Install NPM
Node.js includes NPM automatically when installed
Check version
To verify if it is installed and see the version.
npm --version
List commands
To list all available npm commands.
npm help
Creating a Project
Initialize a project
To start a new project, use npm init
which creates a package.json
file to manage the project’s dependencies and configurations.
npm init
Install all dependencies
This will install all dependencies listed in the package.json
file. This is useful when you clone a repository or share your project with others.
npm install
Dependency Management
Install dependencies
To install a dependency in a project.
npm install package-name
Install development dependencies
Development dependencies are those needed only for development and testing of the project.
npm install --save-dev package-name
Update dependencies
To update a dependency to the latest version.
npm update package-name
Uninstall dependencies
To uninstall a dependency.
npm uninstall package-name
List dependencies
To see the installed dependencies.
npm list
Global installation
To install a package globally (accessible from any project).
npm install -g package-name
Package.json
Description of package.json
Contains metadata about the project, such as the name, version, description, and dependencies of the project.
Example:
{
"name": "my-project",
"version": "1.0.0",
"description": "Description of my project",
"main": "index.js",
"dependencies": {
"express": "^4.17.1",
"lodash": "^4.17.21"
}
}
Scripts
In the package.json
file, you can define custom scripts that run specific commands.
{
"name": "my-project",
"scripts": {
"start": "node index.js",
"test": "jest"
}
}
To run a script:
npm run script-name
Installation and Execution of Tools
Global package installation
The package will be available in all your projects and can be executed from any directory on your system.
npm install -g package_name
List globally installed packages
npm list -g --depth=0
Uninstall global package
To uninstall a package that you’ve installed globally, you can use the following command.
npm uninstall -g package_name
NPX
Run a package without installing it globally.
npx package_command
Verification and Search
Verify package details
npm view package_name
View dependencies of a package
npm view package_name dependencies
Search packages in the NPM registry
npm search keyword
Verify the list of project dependencies
npm ls
Package Publication
Create an account on NPM (if not already have one)
Creating an account on NPM allows you to publish your own packages and manage them from your profile.
npm adduser
Log in to NPM
Logging in to NPM is necessary to perform actions such as publishing packages or accessing your profile from the command line.
npm login
Publish a package on NPM
Publishing a package on NPM allows you to share your code with the community and makes it easier for other developers to use.
npm publish
Update a published package
To update a package already published on NPM, you must first increment the version and then republish it.
npm version type
npm publish
Settings
Global configuration
To view the global configuration of npm.
npm config list -g
Change the registry
You can change the npm registry, for example, to a private server.
npm config set registry https://my-private-registry.com
Version Management
Semver (Semantic Versioning)
NPM uses Semver to manage package versions.
x.y.z
x
: Major version (breaks compatibility with previous versions).y
: Minor version (adds functionality in a backward-compatible manner).z
: Patch version (bug fixes, backward-compatible).
Update Versions
To update the version of a package in package.json
.
npm version major|minor|patch
This will automatically update the version and create a commit and tag in Git.
NPM Configuration
View current NPM configuration
npm config list
Configure a proxy
npm config set proxy http://my.proxy.com:8080
Configure a registry
npm config set registry https://registry.npmjs.org/