WeCodeAcademy

npm and package.json

npm is Node's package manager. A package.json file describes your project and its dependencies. This lesson shows how a package.json is read; the commands are what you run in a terminal.

ExampleNode.js
const pkg = JSON.parse(`{
  "name": "my-app",
  "version": "1.0.0",
  "scripts": { "start": "node index.js", "test": "node --test" },
  "dependencies": { "express": "^4.19.0" }
}`);

console.log(pkg.name, pkg.version);
console.log(Object.keys(pkg.scripts));
for (const [name, range] of Object.entries(pkg.dependencies)) {
  console.log(name, "needs", range);
}
Output
my-app 1.0.0
[ 'start', 'test' ]
express needs ^4.19.0

Useful commands: npm init -y creates a package.json, npm install express adds a package, npm run start runs a script, and npx runs a package without installing it. Packages are stored in the node_modules folder, which you never commit.

Quick check

Which file lists your project's dependencies?

Finish the quick check (and any code exercise) to complete this lesson, or mark it done yourself.

Tip: use the left and right arrow keys to move between lessons.