npm is the package manager that comes with Node.js, and package.json is the file that describes your project and lists the packages it depends on. You use npm to install libraries, run scripts and keep versions consistent between machines. This guide covers the npm package manager from first install to the daily commands you will use.
npm has three parts: a public registry where packages are published, a command-line tool called npm, and the package.json file in each project. When you install a package, npm downloads it from the registry into a folder named node_modules and records it in package.json.
Check that both Node.js and npm are installed:
Create a new folder and run:
The -y flag accepts defaults and writes a starter package.json:
Important fields:
| Field | Purpose |
|---|---|
name and version | Identify the package, needed to publish it |
scripts | Named commands you run with npm run |
dependencies | Packages needed when the app runs |
devDependencies | Packages needed only while developing, such as test tools |
type | Set to module to use ES module import syntax in .js files |
After the first install, package.json gains an entry such as:
Then you can import it:
This example needs "type": "module" in package.json.
Version numbers follow semantic versioning: major.minor.patch. Ranges control which updates npm accepts:
^4.17.21 accepts any compatible 4.x.x version from 4.17.21 upward.~4.17.21 accepts only patch updates, 4.17.x.4.17.21 pins that exact version.The file package-lock.json records the exact version of every installed package, including indirect ones. Commit it to Git so every developer and your build server get the same tree. In automated builds use npm ci, which installs exactly what the lock file says and fails if it does not match package.json.
Never commit node_modules. Add it to .gitignore; anyone can recreate it with npm install.
The scripts field turns long commands into short ones:
start and test can be run without the word run. Locally installed tools are found automatically, so you do not need to install them globally.
Run npm audit regularly, but read the report before using npm audit fix --force, because it may install breaking major versions. Add only packages you trust: check the download count, maintenance activity and the maintainers, because a malicious or abandoned package becomes part of your app.
sudo; use a Node version manager or configure a user-level prefix.--legacy-peer-deps.node_modules and run npm ci.engines field or use a .nvmrc file.npm installs and manages packages. npx runs a package command, downloading it temporarily if it is not installed.
Rarely. Install tools per project so every developer uses the same version.
dependencies are needed at runtime. devDependencies are build or test tools that are not needed in production.
They are alternative package managers using the same registry and similar files. Pick one per project and use only its lock file.
npm downloads packages from a registry into node_modules and records them in package.json. Use npm init to start, npm install to add packages, npm run for scripts and commit package-lock.json for repeatable builds. Keep dependencies few, updated and audited.