🎓 A1Learner
🔍
0 of 50 lessons completed

package.json

package.json is the file that describes a Node project. It holds the project name, version, and tools.

A package.json file

This file describes a project named demo. The version is 1.0.0.

{
  "name": "demo",
  "version": "1.0.0",
  "main": "index.js"
}

Make the file

This command makes package.json with default answers. Run it inside the project folder.

npm init -y

Example

const pkg = require("./package.json");

console.log(pkg.name);
console.log(pkg.version);

Key points

  • Every Node project keeps a file named package.json.
  • The file holds the project name and version.
  • Version numbers carry three parts, like 1.0.0.
  • The file also lists the packages a project uses.
  • Run npm init -y to make the file with default answers.
  • The -y flag accepts the default answers for every question.
  • A flag is an extra word that changes how a command runs.
  • The file uses JSON, a plain text format for data.

In this example, require loads the package.json file into pkg. The program runs in the demo project folder. The first print shows the project name, demo. The second print shows the version, 1.0.0.

Note

Keep package.json in the project folder, next to the code that uses it.

Try it Yourself

Edit the code, then press Run.

Running is simulated

Exercise

What does package.json hold?