liveBook · Manning

Npm

As explained in Secrets of the JavaScript Ninja, Third Edition

The package manager and registry ecosystem commonly used to install, publish, and manage JavaScript packages.

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

How Secrets of the JavaScript Ninja, Third Edition covers it

How deep it goes
Works through initialization, installation, dependency files, and script execution, while deferring full lifecycle-script details to npm documentation.
Examples
Uses an Express HTTP server installation and package.json scripts for greetings and ESLint.
What it assumes
Assumes a Node.js development environment with terminal access while introducing npm project initialization and dependency installation.
What you can do after
Operate/configure it to install dependencies, reproduce a locked dependency tree, and run project scripts.
How it connects
Connects Node.js modules, package.json, node_modules, package-lock.json, version ranges, and package scripts.

Also explained in

Compare how 8 books explain npm →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Secrets of the JavaScript Ninja, Third Edition. The section links point to the parts of the book it draws on.