Read package.json
Local practice: Node.js 22.16.0 and npm 10.9.2 on Linux x64; proposed teaching baseline 26.13.1 LTS remains untested. Use a fresh disposable folder for each example, copy only the shown files and run the fixed command. No packages or external services required. Static examples are not ByteMind runner support. The archive is author-only; do not copy expected.json into the example directory.
Inspect a private project
import { readFileSync } from "node:fs";const manifest = JSON.parse(readFileSync("package.json", "utf8"));console.log(manifest.name, manifest.version, manifest.type);{ "name": "book-report", "version": "1.0.0", "private": true, "type": "module", "scripts": { "report": "node main.mjs" }}Run in this fixture directory:
node main.mjsExpected stdout:
book-report 1.0.0 moduleExpected exit status: 0. Expected stderr: empty. The manifest is parsed into an object; the three selected fields are printed. Reading a file here is a preview of the filesystem chapter.
How it works
name identifies the package, version labels its release, private: true prevents accidental npm publication, and type: module selects ESM for ordinary .js files in that package scope. scripts maps task names to shell commands. engines.node expresses a runtime requirement but is not a universal enforcement mechanism. npm typically warns about incompatible engines unless configured to enforce them. Use strict JSON with double-quoted keys and no trailing commas. The examples inspect the manifest without installing anything.
Read a task without executing it
import { readFileSync } from "node:fs";const { scripts, engines } = JSON.parse(readFileSync("package.json", "utf8"));console.log(scripts.test);console.log(engines.node);{ "name": "book-check", "version": "1.0.0", "private": true, "scripts": { "test": "node --test" }, "engines": { "node": ">=22" }}Run in this fixture directory:
node main.mjsExpected stdout:
node --test>=22Expected exit status: 0. Expected stderr: empty. The test command is merely text at this point. The engine constraint is also read as data.
Common mistake
Do not put credentials in the manifest or mistake private: true for a file-access control. It does not prevent someone with repository access from reading the file.
Try it yourself
Add a description and a report script to a copy of the manifest. Predict which output changes. Explain why changing version does not update the installed Node runtime.
Further reading: Official documentation 1. Related ByteMind reference candidate: node-npm-package-json (route not verified).
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Read package.json — knowledge check
Apply the concepts and predict the behavior taught in this lesson.
Which field prevents ordinary npm publication?
Does reading scripts.test run the test?
Which field chooses ESM for .js in the package scope?