Runtime and core tooling · Lesson 3 of 18
npm and package.json
Describe a project, its scripts and dependencies, and lock versions correctly.
package.json is the project's manifest: name, version, scripts and dependencies. npm install reads it, installs packages into node_modules, and updates package-lock.json, locking the entire dependency tree so CI and teammates reproduce it.
package.json: requested ranges
package-lock: exact tree
npm ci
node_modules
const pkg = {
name: "seacode-api",
version: "1.0.0",
scripts: { start: "node server.js", test: "node test.js" },
dependencies: {}
}
console.log(pkg.name, pkg.scripts.start)dependencies are needed at runtime; devDependencies support development and tests. In CI use npm ci: it requires the lockfile to match and performs a clean install. Commit the manifest and lockfile, not node_modules.
npm init -y npm install express npm install --save-dev eslint npm run test npm ci
Quick question
Which file locks exact versions for the entire tree?
Exercise
Write startCommand(pkg) returning its start script or node index.js as fallback, then print both.
node api.js node index.js
function startCommand(pkg) {
}
console.log(startCommand({ scripts: { start: "node api.js" } }))
console.log(startCommand({}))Learn more from
Played 0 of 2 games
Sign in to save your progress and earn XP.