---
title: "Does npm install run code you never read? Stop install scripts"
description: "With npm install, any package in your dependency tree can run a preinstall, install or postinstall script on your machine. The ignore-scripts setting stops them, and npm's own docs say their only valid use is compiling on the target machine."
url: https://ihsanalapsi.dev/articles/npm-ignore-scripts
lang: en
date: 2026-11-23
updated: 2026-11-23
tags: ["Supply chain security", "Developer tools", "App security"]
author: Ihsan Alapsi (إحسان العبسي)
---
# Does npm install run code you never read? Stop install scripts

You type `npm install` for one package, and dozens of packages you have never heard of come down with it. Any of them can run code on your machine before you open a single file. By the end of this article you will know where that door is, and how to close it with one setting.

## What are install scripts?

Install scripts are commands a package writes in the `scripts` section of its `package.json`, and npm runs them on its own during install, under names such as `preinstall`, `install` and `postinstall`.

The [npm scripts documentation](https://docs.npmjs.com/cli/v11/using-npm/scripts) lists these three among the events that run on `npm install`. It also says that if there is a `binding.gyp` file in the root of a package, npm defaults the `install` command to `node-gyp rebuild` so the code is compiled on your machine.

Think of a parcel that, the moment you open the door to take it, starts something inside your house on its own.

## Why are they a door for supply chain attacks?

Install scripts are a door because they run with your permissions, on your machine or on the CI server, before anyone reads the package's code.

The mistake is human: we trust the package we picked, and we do not think about the packages it pulled in. One stolen publisher account anywhere in the dependency tree is enough, and a new version ships a `postinstall` that reads environment variables and keys and sends them away. For a real example, read [our article on the npm worm and AI tool keys](/articles/npm-testmetest-worm-ai-credentials).

The documentation itself says you should almost never need `preinstall` or `install`, and that their only valid use is compiling code that must be built on the target architecture.

## How do you stop them?

You stop them with the `ignore-scripts` setting: when it is `true`, npm does not run scripts specified in `package.json` files.

The [npm config documentation](https://docs.npmjs.com/cli/v11/using-npm/config) says its default is `false`, so scripts run unless you turn them off. It also says commands meant to run a specific script, such as `npm test` and `npm run`, still run that script, but without the pre and post scripts around it.

```bash
# for one install
npm ci --ignore-scripts

# for every install on this machine
npm config set ignore-scripts true
```

## Where do you start today?

Start where the most keys are: the CI server, then your own machine.

1. Add `--ignore-scripts` to the install command in CI, and run the build and tests as usual.
2. If something fails, find which package. It is usually one that is compiled on the machine, such as one with a `binding.gyp`.
3. For that package, read its script, and decide whether to run it yourself as a separate step, or replace the package.
4. Then turn the setting on for your own machine too, because your machine holds keys the server does not.

This step goes with [locking the versions of your GitHub Actions](/articles/github-actions-workflow-lockfiles): both cut down what runs on your side without you choosing it.

## What do you do now?

Every package you add is code you run, and the default setting lets it run before you read it. Open your CI file now, and check whether the install command has `--ignore-scripts`.

If you want someone to review the supply chain and the CI keys in your project, there is [Muhassan](/muhassan). How many packages in your dependency tree have an install script?

## FAQ

**If I turn scripts off, do npm test and npm run still work?**

Yes. npm's docs say commands meant to run a specific script, such as npm test and npm run, still run it with ignore-scripts, but they do not run the pre and post scripts around it.

**Will some packages break if I turn scripts off?**

Possibly, mostly packages that are compiled on your machine. You find them quickly because the install fails or the package does not work, and you decide on each one.

**Does this protect me from every supply chain attack?**

No. It only closes the door of running code at install time. A malicious package can still run its code when your app imports it, so you still review what you add and pin versions.
