Three days on a calendar
Packages, npm, package.json, semver versions and how to choose dependencies.
A package is a folder with code and a package.json file that its author published so others could use it. Packages live in the npm registry (npmjs.com) — a giant shared warehouse holding millions of them. And the npm program (Node Package Manager) can download, update and remove them.
npm comes with Node.js. Node.js is JavaScript outside the browser: the same language, but without document and window, and with access to files, the network and the terminal instead. Servers, site bundlers, tests — and npm itself — run on it. That's why npm commands are typed in a terminal, not in the browser.
// Terminal (there's no npm in the sandbox — this is what you'll see at home):
// $ npm install dayjs
//
// added 1 package, and audited 2 packages in 1s
// found 0 vulnerabilities
// And then in the project code:
// import dayjs from "dayjs";
// dayjs("2026-10-15").format("dddd, MMMM D, YYYY");
const myDays = 3;
const npmSeconds = 1;
console.log(`Me: ${myDays} days. npm: ${npmSeconds} second.`);What npm install dayjs did:
- downloaded the package from the registry into the project's node_modules folder;
- recorded it in package.json, under dependencies;
- recorded the exact version in package-lock.json.
package.json is the project's passport. It's written in JSON, and JSON is almost a regular JavaScript object. So let's read it right in code:
const packageJson = {
"name": "byte-blog",
"version": "1.0.0",
"scripts": {
"dev": "next dev",
"build": "next build",
"test": "vitest run"
},
"dependencies": {
"dayjs": "^1.11.13"
},
"devDependencies": {
"vitest": "^3.2.4"
}
};
console.log(packageJson.name, packageJson.version);
console.log(Object.keys(packageJson.scripts));
console.log(packageJson.dependencies.dayjs);package.json: name, version, commands and two lists of dependencies.Let's go through the fields:
nameandversion— the name and version of the project itself;scripts— the project's commands:npm run devrunsnext dev,npm run buildruns the build,npm testruns the tests. Nobody memorizes long commands: they're written down here, the same for the whole team;- dependencies — what the program needs to run: the date library ships to your users;
- devDependencies — what only the developer needs: tests, the bundler, the linter. They're installed with
npm install -D vitest.
Now about "^1.11.13". A package version is MAJOR.MINOR.PATCH, and the rules are called semver (semantic versioning): The ^ sign is a permission: "install any 1.x.x that is at least 1.11.13". The tilde ~ is stricter: "only 1.11.x".
- PATCH (
1.11.13 → 1.11.14) — a bug was fixed, nothing else; - MINOR (
1.11.13 → 1.12.0) — something was added, the old stuff works as before; - MAJOR (
1.11.13 → 2.0.0) — something was broken: old code may stop working.
const [major, minor, patch] = "1.11.13".split(".").map(Number);
console.log({ major, minor, patch });
// ^1.11.13 allows: 1.11.14, 1.12.0, 1.99.0
// ^1.11.13 refuses: 2.0.0 — old things may have broken there
// ~1.11.13 allows: only 1.11.14, 1.11.15…But since ^ is a range, I today and a teammate a month from now could get different versions. That's what package-lock.json is for: it records the exact versions of every package — and of the packages those packages depend on. npm install installs exactly those, the same for everyone. The lockfile is kept with the code and never edited by hand.
How to choose a package when there are ten for the same job:
- popularity — weekly downloads on npmjs.com;
- liveliness — when the last release was, whether the authors answer bug reports;
- size — whether it drags another hundred packages along;
- license — MIT, ISC and Apache are fine almost anywhere; for other licenses, read the terms.
// What the famous left-pad did is now a built-in method:
console.log("5".padStart(3, "0"));
console.log("cat".padStart(6, "."));npm install and go take a nap.Pop quiz
Mistakes are fine: keep trying until you find the right answer, and Byte explains why it's right.