package.json is the project record that npm creates and maintains for a Node.js application. It stores metadata such as the name and version, then gains dependency entries when packages are installed. section 2.2
Use package.json to make a JavaScript project easier to set up and reproduce:
- Run npm init in the project root to create the initial file. section 2.2
- Run npm install express to add Express.js as a dependency; npm records it automatically. section 2.2
- Keep package.json while excluding node_modules/ from Git; reinstall the packages later with npm install. section 2.3.2
- Copy package.json and package-lock.json into a container before running npm install for consistent builds. section 5.4.2
Initialize and add Express.js
From the project root, run npm init and accept or edit the prompts. The resulting file includes fields such as name, version, main, scripts, author, and license. Then run npm install express; npm adds an Express.js entry under dependencies. section 2.2
Listing 2.13 Output from npm init
About to write to /Users/justin/Dev/roadtok8s/js/package.json:
{
"name": "js",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC"
}
Inspect the initial package.json generated by npm init, including its metadata and default test script. section 2.2
Listing 2.15 Output from package.json
...
"dependencies": {
"express": "^4.18.2"#1
}
...
See how installing Express.js adds a versioned dependency entry to package.json. section 2.2




