Understanding package.json
Understanding package.json — the core idea
package.json describes a JavaScript package's name, scripts, dependencies, and entry points. Node.js also reads fields such as type and exports to choose loading behavior. TypeScript's tsconfig.json serves a different purpose: compiler and type-checking configuration.
What you’ll learn
- Explain understanding package.json with a concrete use case.
- Run the example and account for every line of its output.
- Recognize the difference between compile-time checking and runtime behavior.
Understand the rules and when to use them
Reading the code: A package manifest is JSON configuration rather than a TypeScript data type. This runnable program merely models a few manifest properties; a real package.json is a file that tools read.
Keep the boundary clear: Setting private:true is a publication safety signal; it does not hide files or implement access control.
See the idea as a flow
Run the complete TypeScript example
Save the code as package-json.ts. The result below was captured after compiling this exact example with strict TypeScript checking and executing the emitted JavaScript in Node.js.
TypeScript code
type PackageInfo = { name: string; version: string; private: boolean };
const manifest: PackageInfo = { name: "task-tools", version: "1.0.0", private: true };
console.log(`${manifest.name}@${manifest.version}`);
console.log(manifest.private);Verified output
task-tools@1.0.0
trueBuild: npx tsc --strict --target es2022 --module commonjs --lib es2022,dom,esnext.disposable package-json.ts; run: node package-json.js. The exact filename may be changed freely. When a multi-file project exists, prefer its tsconfig.json and build scripts.
Trace the example line by line
A package manifest is JSON configuration rather than a TypeScript data type. This runnable program merely models a few manifest properties; a real package.json is a file that tools read.
The output shown above comes from the initialized values in this program. Replacing a type annotation alone does not transform a JavaScript value.
Avoid this common mistake
Setting private:true is a publication safety signal; it does not hide files or implement access control.
Check both the TypeScript diagnostic and the emitted JavaScript behavior. A program can compile correctly and still need runtime validation or error handling.
Practice independently
Create package.json with a build script calling tsc and explain how it differs from tsconfig.json.
Before running your change, write down the type you expect TypeScript to infer and the output you expect JavaScript to print. Then check both predictions.
Check your understanding
What to remember
package.
Try the practice change and re-run the exact program to test your understanding.
Reaching the recap and completing the quizzes are separate milestones.