NPM & package.json
npm (Node Package Manager) ships with Node itself, and package.json is the file that describes your project — its dependencies, its scripts, and basic metadata.
Starting a project
bash terminal
npm init -y
Generated package.json
{
"name": "my-app",
"version": "1.0.0",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
}
}The -y flag accepts all the defaults instead of asking a series of questions — fine for getting started, easy to edit afterward.
Installing a package
bash terminal
npm install express
Output
added 64 packages in 2s
This creates a node_modules folder (the actual package code), a package-lock.json (exact installed versions, for reproducible installs), and adds an entry to package.json:
package.json, after install
"dependencies": {
"express": "^4.19.2"
}npm scripts
The scripts section defines shortcuts you run with npm run (or just npm start/npm test for those two specific names):
JSON package.json (scripts section)
"scripts": {
"start": "node index.js",
"dev": "node --watch index.js"
}
bash terminal
npm start
Output
> my-app@1.0.0 start > node index.js Server running at http://localhost:3000/
Note:
node_modules should never be committed to git — it can be entirely regenerated from package.json and package-lock.json with npm install, and it's often huge. package-lock.json, on the other hand, should be committed — it's what guarantees everyone on a team, and your production server, installs the exact same dependency versions.