Beginner Fundamentals

Understanding package.json

The package.json file is the heart of a Node.js project. It describes your project and lists its dependencies and scripts.

Common fields

// package.json (shown as an object)
{
  "name": "my-app",
  "version": "1.0.0",
  "description": "A small Node.js project",
  "main": "app.js",
  "scripts": {},
  "dependencies": {},
  "devDependencies": {}
}
  • name and version identify the project.
  • main points to the entry file.
  • dependencies are needed to run the app.
  • devDependencies are only needed while developing.

Scripts

The scripts field defines commands you can run with npm run:

{
  "scripts": {
    "start": "node app.js",
    "test": "jest"
  }
}

Run them with npm start or npm run test. The start and test names are special and can be run without run.

Summary

package.json keeps your project organized: metadata, dependencies, and handy scripts all in one file.