← Back to Node.js Course | Chapter 3: npm & Packages | Lesson 1 of 7

package.json

package.json is the ID card of your project: its name, version, scripts and dependencies.

In this page:

  1. package.json
Syntax
javascript
{
  "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

javascript
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
  1. Writing invalid JSON with trailing commas or comments
  2. Forgetting the scripts entry
  3. 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:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.