liveBook · Manning

Package.json

As explained in Road to Kubernetes

package.json is a project configuration file that records a package's metadata, dependencies, version, and scripts.

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

How Road to Kubernetes covers it

How deep it goes
Explains initialization, metadata, automatic dependency updates, and container copying, but does not detail the complete file specification.
Examples
Uses npm init, npm install express, package-lock.json, node_modules, and a Node.js Dockerfile as concrete artifacts.
What it assumes
Assumes Node.js and npm installation, while teaching project initialization and dependency tracking through runnable commands.
What you can do after
Operate/configure Node.js projects by initializing package.json, adding Express.js, restoring dependencies, and preparing container builds.
How it connects
Connects package.json with npm, Node.js, Express.js, node_modules, package-lock.json, Git, and Docker.

Also explained in

Compare how 5 books explain package.json →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Road to Kubernetes. The section links point to the parts of the book it draws on.