Join our Newsletter — 33% off our NHI Course
Home› Glossary› Cyber Security› npm Preinstall Script
Cyber Security

npm Preinstall Script

← Back to Glossary
By NHI Mgmt Group Updated October 28, 2026 Domain: Cyber Security

An npm preinstall script is a package script that runs automatically before a package is installed. It executes during the installation lifecycle, often with the same privileges as the installing user or build process, so malicious or unexpected code in the script can run before the package contents are fully trusted.

What this script does in the npm install lifecycle

An npm preinstall script is executed before package installation completes, so it sits at one of the earliest and least trusted points in the dependency lifecycle. That timing makes it powerful for setup, but also dangerous when package origin or integrity is uncertain.

Because the script runs automatically, its behaviour is not a passive property of the package, it is active code execution during installation. In practice, that means a package can do work before its contents are fully reviewed, pinned, or even fully unpacked into the environment.

This is why preinstall hooks are often discussed alongside malicious package behaviour, dependency compromise, and install-time abuse rather than normal application logic. The security question is not whether the hook is syntactically valid, but whether the code should be allowed to run at all.

Why preinstall scripts are risky

The main risk is that installation-time code inherits the trust and reach of the installation context. If a developer, CI job, or build agent installs a package with a preinstall hook, the hook may read local files, inspect environment variables, or stage follow-on actions before defenders have a chance to inspect the package manually.

That matters because the install phase often contains valuable material, including credentials, tokens, build configuration, source trees, and cached secrets. A malicious preinstall script can exploit that moment to exfiltrate or tamper with data while looking like ordinary package setup.

A useful reference point is the Malicious Open Source Package Response Playbook, which focuses on the practical response pattern when a package is suspected of running harmful install-time code. The broader class of incidents captured in the coa and rc npm hijacks 2021 shows how npm abuse can turn a normal dependency event into credential theft.

How organisations reduce exposure

The first control is visibility: know which packages, lockfiles, build images, and install paths your organisation actually uses. Preinstall scripts are much easier to govern when dependency intake is inventory-driven rather than ad hoc.

Then reduce the chance that scripts execute blindly. Review package-manager settings, enforce frozen or locked dependency resolution where possible, and prefer installation modes that suppress or gate lifecycle scripts in high-trustless contexts. The npm Install-Time Security Guide is a direct navigation aid for those decisions, especially where script allowlists, frozen lockfiles, and registry controls are part of the response.

For broader coverage, organisations should pair package controls with source and build hygiene. That includes limiting where secrets are available during install, separating developer workstations from build runners, and making sure trusted publishing or equivalent release controls are used so package updates are harder to abuse.

Detection and response when a preinstall hook is suspicious

Suspicious install-time code is often detected indirectly, through unexpected network calls, process execution, file writes, or secret access during dependency installation. Because these scripts run early, the signal may appear in build logs, endpoint telemetry, or container audit trails rather than in application logs.

When compromise is suspected, the important question is not only whether the package should be removed, but whether anything reachable from that installation was exposed. The response may need to include revoking publishing tokens, rotating API keys and session material, and rebuilding affected environments from known-good sources.

Case material such as the eslint-scope npm compromise 2018 and the Rspack npm compromise 2024 illustrates why post-install review must include credential hygiene, not just package removal. Once install-time code has executed, the trust boundary has already been crossed.

What the term means for software supply-chain governance

Preinstall scripts are not inherently malicious, but they are a supply-chain control point that demands policy. Teams should decide which packages are allowed to run lifecycle scripts, where that allowance is granted, and who owns exceptions when a dependency requires install-time execution.

The governance lesson is that dependency trust is not the same as package popularity or source reputation. A package can be widely used and still be an unsafe install-time actor if its lifecycle hooks are excessive, opaque, or unnecessary for the build.

Where install-time behaviour is part of normal operations, the safer pattern is explicit approval, strong provenance, and tight control over the environment in which the script runs. That keeps the convenience of package automation from becoming an unmanaged execution channel.

Standards & Framework Alignment

This section maps relevant standards and security frameworks to the operational risks and controls described in this guidance.

OWASP Non-Human Identity Top 10 addresses the attack and risk surface, while CIS Controls v8, NIST SP 800-53 Rev 5 and OWASP ASVS set the governance and control requirements practitioners need to meet.

FrameworkControl / ReferenceRelevance
CIS Controls v8CIS-5 — Account ManagementLifecycle scripts can expose credentials and require controlled access paths.
Recommendation — Restrict package-install execution paths and revoke any exposed accounts or tokens quickly.
NIST SP 800-53 Rev 5SI-3 — Malicious Code ProtectionPreinstall hooks are a malicious-code execution vector during package install.
CM-5 — Access Restrictions for ChangeAllowing lifecycle scripts changes trusted execution during software installation.
Recommendation — Block or inspect install-time code before it executes in build and developer environments. Limit who can approve install-time script execution and under what conditions.
OWASP ASVSV15 — Secure Coding and ArchitectureInstall-time code is a software trust boundary that secure design should constrain.
Recommendation — Design dependency handling so package lifecycle hooks cannot execute without review or policy.
OWASP Non-Human Identity Top 10NHI-02 — Secret LeakageMalicious install scripts often target secrets reachable during installation.
Recommendation — Prevent package-install code from reading or exfiltrating secrets during setup.

Free weekly newsletter

Subscribe to the NHI & AI Identity Journal

The latest on NHI and Agentic AI security – articles, research, breaches, news and events every week.

Bonus 33% off our NHI Course when you subscribe.

NHIMG Editorial Note
Reviewed and updated by the NHIMG editorial team on October 28, 2026.
NHI Mgmt Group — the #1 independent authority on Non-Human Identity, IAM, and Agentic AI security. nhimg.org