npm (Node Package Manager) installs open-source packages from npmjs.com — over two million of them.
Start a project
npm init -y creates package.json, the file that describes your project: name, version, scripts and dependencies.
Install packages
npm install express— a dependency your app needs to runnpm install -D nodemon— a dev dependency (only for development)npm install— install everything listed in package.jsonnpm uninstall express,npm update,npm outdated
Packages are saved in the node_modules folder. Never edit it and never commit it to Git — add it to .gitignore.
package-lock.json
Records the exact versions installed, so everyone gets the same versions. Do commit this file. Use npm ci on servers for a clean, exact install.
Scripts
Add commands in "scripts" and run them with npm run <name> (npm start and npm test work without run).
npx
npx <tool> runs a package without installing it globally, e.g. npx create-vite@latest.
Versions
^4.21.0 allows minor updates (4.x), ~4.21.0 allows only patch updates (4.21.x).
Example
{
"name": "my-api",
"version": "1.0.0",
"type": "module",
"main": "src/index.js",
"scripts": {
"start": "node src/index.js",
"dev": "node --watch src/index.js",
"test": "node --test"
},
"dependencies": {
"express": "^4.21.0"
},
"devDependencies": {
"nodemon": "^3.1.0"
}
}
npm run dev starts the app and restarts it when files change; npm start runs it normally. express is needed in production, nodemon only while developing.