What it is
npm is the default package manager for Node.js, used for installing, sharing, and managing JavaScript packages and their dependencies.
Installation
Linux:
sudo apt update
sudo apt install nodejs npm
macOS:
brew install node
Windows:
Download the installer from https://nodejs.org/ and run it. npm is included with Node.js.
Core Concepts
package.json: The manifest file for a Node.js project. It contains metadata about the project, including its name, version, dependencies, scripts, and more.- Dependencies: Packages that your project needs to run. These are listed in
package.json.dependencies: Packages required for your application to run in production.devDependencies: Packages required only for development and testing (e.g., linters, build tools).
node_modules: The directory where npm installs all the project’s dependencies. This directory is usually excluded from version control.npm registry: A large public database of JavaScript packages hosted on npmjs.com.- SemVer (Semantic Versioning): A versioning scheme (MAJOR.MINOR.PATCH) used for packages, allowing for predictable updates.
Commands / Usage
Project Initialization & Management
-
npm initInitialize a new Node.js project and create apackage.jsonfile.npm init -yCreates a
package.jsonwith default values. -
npm init <initializer>Initialize a project using a specific initializer (e.g., a framework’s boilerplate).npm init react-app my-react-appCreates a new React application in the
my-react-appdirectory.
Installing Packages
-
npm install <package-name>Install a package and its dependencies. Adds it todependenciesinpackage.jsonand installs it intonode_modules.npm install lodashInstalls the latest version of lodash.
-
npm install <package-name>@<version>Install a specific version of a package.npm install react@17.0.2Installs React version 17.0.2.
-
npm install <package-name> --save-dev(or-D) Install a package as a development dependency. Adds it todevDependenciesinpackage.json.npm install jest --save-devInstalls Jest for testing.
-
npm installInstall all dependencies listed inpackage.json.npm installInstalls all packages defined in
node_modules. -
npm uninstall <package-name>Remove a package fromnode_modulesand its entry frompackage.json.npm uninstall expressRemoves the Express package.
-
npm install -g <package-name>Install a package globally, making it available as a command-line tool.npm install -g nodemonInstalls nodemon globally.
-
npm uninstall -g <package-name>Uninstall a globally installed package.npm uninstall -g nodemonUninstalls nodemon.
-
npm updateUpdate all packages to the latest allowed versions according topackage.json’s version ranges.npm updateUpdates packages like
lodashto their latest compatible versions. -
npm update <package-name>Update a specific package.npm update lodashUpdates lodash to the latest compatible version.
Running Scripts
-
npm run <script-name>Execute a script defined in thescriptssection ofpackage.json.npm run buildRuns the
buildscript defined inpackage.json. -
npm startRuns thestartscript (a common convention).npm startExecutes the
startcommand frompackage.json. -
npm testRuns thetestscript (a common convention).npm testExecutes the
testcommand frompackage.json.
Package Information
-
npm listList installed packages and their versions.npm listShows all installed packages in the current project.
-
npm list -g --depth=0List globally installed packages.npm list -g --depth=0Shows top-level global packages.
-
npm view <package-name>Display information about a package from the registry.npm view react versionsShows all available versions of the React package.
-
npm view <package-name> descriptionDisplay the description of a package.npm view express descriptionShows the description of the Express package.
Other Useful Commands
-
npm clean-installRemovenode_modulesand reinstall all dependencies based onpackage.jsonandpackage-lock.json. Useful for ensuring a clean slate.npm clean-installDeletes
node_modulesand reinstalls everything. -
npm cache clean --forceClear the npm cache. Sometimes necessary if you encounter strange installation issues.npm cache clean --forceClears the npm cache directory.
-
npm outdatedCheck for outdated packages.npm outdatedLists packages that have newer versions available.
-
npm pruneRemove extraneous packages (those not listed inpackage.json).npm pruneRemoves any packages in
node_modulesthat aren’t required bypackage.json.
Common Patterns
-
Installing a specific version and saving it:
npm install moment@2.29.1 -
Globally installing a command-line tool:
npm install -g http-serverThen run it from any directory:
http-server -
Running a build script defined in
package.json:# Assuming you have "build": "webpack --mode production" in your scripts npm run build -
Executing a script with arguments:
# Assuming you have "lint": "eslint ." in your scripts npm run lint -- --fixThe
--separates npm arguments from script arguments. -
Installing dependencies from a
package-lock.json(ensures reproducible builds):npm ciThis command is recommended for CI/CD environments and team collaboration. It’s faster than
npm installand strictly adheres to the lock file. -
Creating a tarball of your package:
npm packCreates a
.tgzfile in the current directory for distribution.
Gotchas
- Global vs. Local Installs: Packages installed with
-gare available system-wide as executables, but are not available to your project’s code directly. Project dependencies should always be installed locally. package-lock.json: This file is crucial for reproducible builds. It locks down the exact versions of all installed packages, including dependencies of dependencies. Always commit it to version control.npm ciuses this file.npm updatevs.npm install <package>@latest:npm updaterespects the version ranges inpackage.json(e.g.,^1.2.3might update to1.3.0but not2.0.0).npm install <package>@latestwill fetch the absolute latest version and updatepackage.jsonaccordingly.- Cache Issues: If you encounter persistent installation errors, try
npm cache clean --force. - Permissions Errors (Global Installs): On Linux/macOS, installing globally might require
sudoif npm is installed in a system directory. However, it’s often better to configure npm to use a user-owned directory for global installs to avoid permission issues. npm audit: Usenpm auditto check for known security vulnerabilities in your dependencies.npm audit fixattempts to resolve them.