# npm

`cooldown` gates npm projects through `--tool npm` (aliases `node`, `js`, `javascript`,
`typescript`).

## What it reads

An npm project is a `package.json` with its `package-lock.json`.

npm's `package-lock.json` is read in `lockfileVersion` 2 or 3, which npm 7 and newer write. A
`lockfileVersion` 1 lock, written by npm 6 and older, records its nested copies only inside a
`dependencies` tree. So it is reported `lockfile_unreadable`, with a message to re-lock with npm 7
or newer, rather than read in part.

## Registry

Publish times come from the npm registry.

## How it is driven

`cooldown` runs `npm`, or the program `COOLDOWN_NPM` names:

- **`check`:** proves the lock current, as described below.
- **`upgrade` and `fix`:** land each target in the lock, as
  [`upgrade`](https://romnn.github.io/cooldown/docs/commands/upgrade/#lock-versus-manifest) describes, then verify the
  lock they wrote the same way `check` does.
- **`--build`:** runs `npm install --no-audit --no-fund`.

npm has no lock-only refresh, so `check --lock` and `outdated --lock` only warn and read the lock
as it is.

### How `check` proves the lock current

`check` asks npm itself whether the lock is current. It needs npm 10.5.1 or newer, since older
releases remove `node_modules` despite `--dry-run`: an older npm leaves the lock `lock_unknown`.

It runs `npm ci --dry-run --ignore-scripts` in the project, which installs nothing, with npm's
cache and logs kept in `cooldown`'s cache directory. So the project, its `node_modules` included,
stays byte for byte as it was.

npm's verdict decides the result:

- **A refusal of a lock out of sync with `package.json`, or of a missing lock:** a stale lock that
  `--allow-stale-lock` waives.
- **Any other npm failure:** `tool_failed`.

Under `--offline` npm runs offline too. It still proves a current lock from the lock alone, but a
stale lock whose resolution needs registry metadata npm has not cached is `tool_failed` rather than
stale.

## Edge cases

npm compares versions only, so the lock is `lock_unknown` rather than current wherever its check
does not reach:

- an `npm-shrinkwrap.json` beside the lock, which npm verifies instead;
- npm resolving the project into an enclosing workspace, or a configured `workspace` filter (in an
  `.npmrc` or `npm_config_workspace`), which narrows `npm ci` to some members;
- a declaration of the root, a workspace member, or a `file:` package that differs from what the
  lock recorded for it, such as a registry range changed to a tarball URL of the same version;
- an `overrides` entry that is neither a version range nor a `$` reference to a root declaration
  that is itself a range, such as an `npm:` alias, a URL, a git source, or a local path, since the
  lock records no override.

Every npm-family tool owns `package.json`, so two of them at one root take turns under `upgrade`,
`fix` and `--lock`; see [Several ecosystems at once](https://romnn.github.io/cooldown/docs/tools/#several-ecosystems-at-once).

## Fixing what goes wrong

- **A `lockfile_unreadable` lock:** re-lock with npm 7 or newer.
- **A stale lock:** re-lock with `npm install`, since `check --lock` has no npm refresh.
