registry.npmjs.orglatestms 2.1.1ms 2.1.2ms 2.1.3hello-npm/node_modules/ms 2.1.31{2 "name": "hello-npm",3 "version": "1.0.0",4 "scripts": { "start": "node index.js" },5 "dependencies": { "ms": "^2.1.3" }6}1"node_modules/ms": {2 "version": "2.1.3",3 "integrity": "sha512-6F…"4}1const ms = require("ms");2console.log(ms("2 days"));Terminal · hello-npm$ npm init -y$ npm install ms$ npm start172800000$ npm cims 2.1.3^2.1.3"ms"172800000
Back: the npm registry and your project's node_modules folderFront: package.json, package-lock.json, index.js and the terminalTokens: commands, the request, the ms 2.1.3 package, the range, the lock, the function and the resultAt the back, the npm registry with three recent releases of ms, and your project's node_modules folder. In the middle, package.json and package-lock.json; in front, index.js and the terminal. npm install ms downloads the latest release into node_modules, saves the range ^2.1.3 in package.json and pins 2.1.3 with its hash in package-lock.json. npm start runs index.js, require finds ms in node_modules, and npm ci rebuilds node_modules exactly from the lock.

Packages and the registry

  1. A package is a folder of reusable code with a package.json file that names it. npm, the package manager installed with Node.js, downloads packages from the npm registry, a huge public online library of them.

Installing a package

Using it

Sharing the project

What happens

// step 1/10 · registry
package    a folder of code + its own package.json
npm        the package manager installed with Node.js
registry   registry.npmjs.org, the public library
example    ms: turns "2 days" into milliseconds
recent     2.1.1, 2.1.2, 2.1.3 (latest 2.1.3)

npm init -y also writes description, main, keywords, author, license, type and a placeholder test script; the card shows only the fields this lesson uses, and npm writes each object over several lines. require is Node's CommonJS module system. With "type": "module" in package.json you would write import ms from "ms", the standard ECMAScript import; Node finds the package in node_modules the same way.