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 --versionList commands
To list all available npm commands.
npm helpCreating 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 initInstall 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 installDependency Management
Install dependencies
To install a dependency in a project.
npm install package-nameInstall development dependencies
Development dependencies are those needed only for development and testing of the project.
npm install --save-dev package-nameUpdate dependencies
To update a dependency to the latest version.
npm update package-nameUninstall dependencies
To uninstall a dependency.
npm uninstall package-nameList dependencies
To see the installed dependencies.
npm listGlobal installation
To install a package globally (accessible from any project).
npm install -g package-namePackage.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-nameInstallation 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_nameList globally installed packages
npm list -g --depth=0Uninstall global package
To uninstall a package that you’ve installed globally, you can use the following command.
npm uninstall -g package_nameNPX
Run a package without installing it globally.
npx package_commandVerification and Search
Verify package details
npm view package_nameView dependencies of a package
npm view package_name dependenciesSearch packages in the NPM registry
npm search keywordVerify the list of project dependencies
npm lsPackage 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 adduserLog 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 loginPublish 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 publishUpdate 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 publishSettings
Global configuration
To view the global configuration of npm.
npm config list -gChange the registry
You can change the npm registry, for example, to a private server.
npm config set registry https://my-private-registry.comVersion Management
Semver (Semantic Versioning)
NPM uses Semver to manage package versions.
x.y.zx: 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|patchThis will automatically update the version and create a commit and tag in Git.
NPM Configuration
View current NPM configuration
npm config listConfigure a proxy
npm config set proxy http://my.proxy.com:8080Configure a registry
npm config set registry https://registry.npmjs.org/