NPM is the package manager for Node.js, used to install, share, and manage dependencies for Node.js and JavaScript projects.
NPM Installation
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
Project Creation
Initialize a project
To start a new project, use npm init
which creates a package.json
file to manage the project’s dependencies and settings.
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 dev dependencies
Dev dependencies are those needed only for the 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 view the installed dependencies
npm list
Global installation
To install a package globally (accessible from any project)
npm install -g package-name
Package.json
Contains metadata about the project, such as the name, version, description, and project dependencies
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, custom scripts can be defined to execute 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 in your system
npm install -g <package_name>
List installed global packages
npm list -g --depth=0
Uninstall global package
To uninstall a package that you have 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
Check package details
npm view <package_name>
View package dependencies
npm view <package_name> dependencies
Search packages in the NPM registry
npm search <keyword>
Verify the list of project dependencies
npm ls
Package Publishing
Create an account on NPM (if not already)
Creating an account on NPM allows you to publish your own packages and manage them from your profile.
npm adduser
Login 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 make 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
Configurations
Global configuration
To view the global npm configuration
npm config list -g
Change registry
The npm registry can be changed, 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 backwards-compatible functionality).z
: Patch version (bug fixes, backwards-compatible).
Version Updates
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 the current NPM configuration
npm config list
Set up a proxy
npm config set proxy http://my.proxy.com:8080
Set up a registry
npm config set registry https://registry.npmjs.org/