A JavaScript project often needs code written by other people. npm is Node.js’s package manager, paired with a public registry for sharing packages. It installs dependencies for server projects and can also help manage dependencies, tests, and builds for browser-targeted code. section 3.2 section 3.0
Start a small HTTP server project
In a new directory, run `npm init -y` to create `package.json`, then `npm install express@5`. npm downloads Express and its dependency tree from the registry. The packages appear in `node_modules`; `package.json` records Express as a direct dependency, and `package-lock.json` records the installed tree. Code in `index.mjs` can then import Express by package name. section 3.2.1
The two package files answer different questions. `package.json` permits dependency version ranges: installing Express 5, for example, can produce a caret-prefixed version. `package-lock.json` specifies exact versions throughout the tree. Commit the lockfile rather than `node_modules` so another `npm install` can reconstruct the same tree. A library’s lockfile has a caveat: it fixes versions during local development but does not fix the dependency tree installed by projects that consume the library. section 3.2.3
npm also provides commands for routine project work:
- Define commands under `scripts` in `package.json` and invoke them with `npm run <script>`. Installed package executables are available to those scripts, so a lint script can run `eslint .`. section 3.3.1
- Pass script arguments after `--`. Hooks such as `prebuild` and `postbuild` run around `build`; installation can trigger lifecycle scripts. section 3.3.1
- Use `npx` to run a package executable; if its package is absent, it can be downloaded and installed temporarily for the command. section 3.4







