npm module manages Node.js packages using npm (Node Package Manager).
Functions
packages
Install, remove, or update npm packages.str | list[str]
List of packages to ensure.
bool
default:true
Whether the packages should be present.
bool
default:false
Whether to upgrade packages without a specified version.
str
Directory to manage packages for. Defaults to global installation.
Package versions can be pinned like npm:
<pkg>@<version>Examples
Common Use Cases
Express.js Application Setup
React Application
Global Development Tools
Install from package.json
Build Tools
Testing Framework
npm-Specific Tips
Global vs Local Packages
Global vs Local Packages
Global packages (no
directory parameter):- Installed system-wide
- Typically CLI tools
- Use
npm install -g
directory parameter):- Installed in project’s
node_modules - Project dependencies
- Use
npm installin directory
Using package.json
Using package.json
For full project setups, use package.json:
Update Packages
Update Packages
Update packages to latest versions:
Scoped Packages
Scoped Packages
Install scoped packages (organization packages):
Clean Install
Clean Install
Perform a clean install (removes node_modules):
Private Registries
Private Registries
Install from private npm registries:
Best Practices
Use package-lock.json
Use package-lock.json
Always commit and deploy package-lock.json for reproducible builds:
Separate Dev and Production
Separate Dev and Production
Pin Major Versions
Pin Major Versions
Important Notes
When no
directory is specified, packages are installed globally using npm install -g.