package.json
package.json is the ID card of your project: its name, version, scripts and dependencies.
In this page:
Syntax
{
"name": "package-name",
"version": "1.0.0",
"main": "index.js",
"scripts": {
"start": "node index.js"
},
"dependencies": {}
}
package.json
The file lists metadata (name, version, description), the entry point main, custom scripts, and dependency maps. npm reads it to install packages and run scripts. Setting type to module switches .js files to ES modules.
Note:
Run npm init -y to generate a starter file quickly.
Example: package.json
const pkg = {
name: "demo-app",
version: "1.0.0",
main: "index.js",
scripts: { start: "node index.js", test: "node --test" },
dependencies: { express: "^4.19.2" },
devDependencies: { nodemon: "^3.1.0" },
};
console.log(JSON.stringify(pkg, null, 2));
console.log("scripts:", Object.keys(pkg.scripts));
// Output:
// {
// "name": "demo-app",
// "version": "1.0.0",
// "main": "index.js",
// "scripts": {
// "start": "node index.js",
// "test": "node --test"
// },
// "dependencies": {
// "express": "^4.19.2"
// },
// "devDependencies": {
// "nodemon": "^3.1.0"
// }
// }
// scripts: [ 'start', 'test' ]
⚠️ Run this in your own terminal or Node.js environment.
Related Topics
Common Mistakes
- Writing invalid JSON with trailing commas or comments
- Forgetting the scripts entry
- Leaving the default test script that always fails
Chapter Summary
- package.json describes the project
- scripts define npm run commands
- dependencies are listed by name and range
- type controls module format
🔒
Chapter Quiz — Complete all 7 topics to unlock
0/7 topics done
Complete these topics first: