DEV Community

Sandscale
Sandscale

Posted on Originally published at sandscale.dev

npm ci for coding agents: lockfile-only installs, and what to do about install scripts

In a Node repository, the first thing a coding agent does after cloning is install dependencies. The command it picks matters more than it looks. npm install is allowed to change package-lock.json, to resolve a range to a newer version, and to run install scripts from every package it fetches. For a person at a laptop who is watching, that is usually fine. For an agent working unattended on a branch nobody has read, it is a choice worth making on purpose.

This post is about making the install step predictable for a team's agents. The npm behaviour comes from the npm ci documentation (version 10, read on 2 December 2026). Where Sandscale appears, the commands are from CLI 1.4.1.

What npm ci does differently

From the npm documentation, npm ci is meant for automated environments, "any situation where you want to make sure you're doing a clean install of your dependencies". The differences from npm install that matter for agents:

  • It needs a lockfile. The project must have a package-lock.json or npm-shrinkwrap.json.
  • It refuses to disagree. If the dependencies in the lockfile do not match package.json, npm ci exits with an error instead of updating the lockfile.
  • It never writes your files. It will never write to package.json or any lockfile. Installs are, in the documentation's words, essentially frozen.
  • It starts clean. If a node_modules already exists, it is removed before the install begins.
  • It installs a whole project only. You cannot add a single dependency with it.

For an agent, the third point is the useful one. The lockfile in the pull request is the one a person committed, not one the agent regenerated at 3 a.m. by running npm install after a failing test made it curious.

The rule for AGENTS.md

## Dependencies (Node)
- Install with `npm ci`. Never run `npm install` or `npm update`.
- Do not add, remove or upgrade a package unless the task says to. If you think one is needed,
  stop and say which package and why.
- If `npm ci` fails because the lockfile and package.json disagree, stop and report it.
  Do not fix it by running `npm install`.
- Do not edit package-lock.json by hand.
Enter fullscreen mode Exit fullscreen mode

Instruction files are requests, not enforcement. If you want enforcement, a hook can rewrite the command; our post on routing Claude Code's Bash commands to a sandbox shows the mechanism and includes npm ci in its pattern. A committed .npmrc can also hold settings the team wants everywhere.

Install scripts: the part npm ci does not change

npm ci is frozen to the lockfile, but it still runs the install scripts of the packages in it, unless you turn them off. A package's preinstall, install and postinstall scripts run code on the machine. That is the supply chain risk our post on agents installing packages describes, and it is why the machine the agent runs on matters.

npm has an ignore-scripts setting. When it is true, npm does not run the scripts in package.json files, with one caveat from the documentation: commands explicitly meant to run a script, such as npm test and npm run, still run the script they were asked to run. The practical use:

npm ci --ignore-scripts
Enter fullscreen mode Exit fullscreen mode

Some packages need their install scripts to work: native modules that compile, or tools that download a binary. If the repository has a few of them, the team can allow them on purpose. Run npm ci --ignore-scripts first, then run npm rebuild <package> for the packages you have decided to trust, and write that list in the repository so the agent does not guess. We cannot tell you which packages that is for your project, and an agent should not decide it either.

Where the install runs

The install should run where an unexpected script does the least harm. On a laptop, a script runs as you, next to your SSH keys and cloud credentials. On a Sandscale workspace, each agent has its own machine, a Firecracker microVM with its own disk, so a script that misbehaves is working in a machine that holds that agent's checkout and nothing of yours.

sandscale workspace create --name web-3318-search --template-id TEMPLATE_ID --ttl-seconds 28800
sandscale workspace ssh web-3318-search
Enter fullscreen mode Exit fullscreen mode

An install needs to reach the package registry, so the machine needs the network access that your plan and your organization's policy allow. If the agent's installs fail on the network, that is the first thing to check. Our post on egress allowlists covers keeping that access short.

Because the workspace disk persists, the install happens once per machine, not once per task. An agent that starts a task on a machine that already has node_modules will be running npm ci anyway, and npm ci will remove and recreate it, so the second install pays the whole cost again. If that is too slow for your repository, that is a reason to run npm ci once at the start of a task and not at every attempt.

What the reviewer checks

Because the agent is not allowed to change the lockfile, a changed lockfile in a pull request is a signal. Put this on your reviewer checklist:

  • Is package-lock.json in the diff? If the task did not say to change a dependency, ask why.
  • If it changed, is each change one the task asked for? A lockfile diff is long. Look at which packages were added or had their versions changed, not at every line.
  • Did package.json gain a scripts entry, or a preinstall or postinstall? Those run on everyone's machine.
  • Did .npmrc change?

What this does not cover

It does not make the packages in your lockfile trustworthy. A lockfile freezes what you chose; it does not review it. Auditing and pinning policies are separate work, and npm ci only keeps the agent from changing them quietly.

The short version for a team: install with npm ci, turn scripts off unless a package needs them, run the install on the agent's own machine, and treat a changed lockfile as something to explain.

Originally published on the Sandscale blog.

Top comments (0)