JavaScript applications depend on external packages for almost everything, including UI components, authentication, database connectivity, logging, testing, and build automation.
Most developers install these dependencies using npm without thinking too much about what happens after a package is downloaded.
However, installing an npm package can execute code automatically through lifecycle scripts. These scripts may run during installation and perform operations on the developer's machine or inside a CI/CD environment.
This creates a security risk when a dependency comes from an untrusted source or contains malicious code.
For example, a compromised package could execute a script that reads environment variables, accesses sensitive files, or attempts to collect authentication tokens during installation.
npm 12 introduces stronger installation security controls that change how dependency lifecycle scripts are handled. Instead of automatically executing every dependency's installation scripts, npm uses an explicit approval mechanism.
In this article, we will explore how npm lifecycle scripts work, how npm 12 protects applications from untrusted installation scripts, and how to configure trusted dependencies without breaking the development workflow.
Understanding npm Package Lifecycle Scripts
An npm package can define scripts in its package.json file.
These scripts are used to perform operations during different stages of a package's lifecycle.
Consider the following package.json file:
{
"name": "sample-library",
"version": "1.0.0",
"scripts": {
"preinstall": "node scripts/check.js",
"install": "node scripts/install.js",
"postinstall": "node scripts/setup.js"
}
}When npm installs this package, the lifecycle scripts can execute automatically if the installation policy permits them.
The most important installation-related lifecycle scripts include:
Script | Purpose |
|---|---|
preinstall | Runs before the package installation process |
install | Runs during package installation |
postinstall | Runs after the package installation |
prepare | Runs during package preparation, including certain Git dependency workflows |
These scripts are useful for legitimate purposes.
For example, some packages use installation scripts to download platform-specific binaries, compile native modules, or prepare generated files.
However, the same mechanism can also execute malicious commands.
The problem is not that lifecycle scripts are inherently unsafe. The problem is allowing arbitrary third-party code to execute automatically without reviewing whether it is necessary.
Why Automatic Installation Scripts Create Security Risks
Consider a development team working on a JavaScript application.
The project depends on several external packages, including packages maintained by different developers and organizations.
When a developer runs npm install, the package manager downloads the dependencies and may execute their installation scripts.
If one dependency has been compromised, its installation script could execute unexpected operations.
For example:
{
"scripts": {
"postinstall": "node scripts/setup.js"
}
}The setup.js file could perform legitimate configuration tasks, but it could also contain code that accesses sensitive environment variables or communicates with an external server.
The developer may never inspect this file because the installation process executes it automatically.
This becomes especially dangerous in CI/CD pipelines where environment variables may contain:
Private package registry credentials.
Cloud authentication tokens.
Deployment credentials.
API keys.
Repository access tokens.
A malicious installation script may attempt to access these resources using the permissions available to the installation process.
This is why dependency installation should be treated as a security-sensitive operation.
Understanding npm 12 Installation Security
npm 12 introduces stronger controls for dependency installation.
One of the most important changes is that dependency lifecycle scripts are blocked by default unless they are explicitly approved.
The allowScripts configuration controls which dependencies are permitted to execute installation scripts.
This means developers can install packages without automatically trusting every dependency that contains lifecycle scripts.
npm 12 also provides commands for reviewing and managing these approvals.
Another important change is that Git dependencies and remote URL dependencies are restricted by default through separate installation controls.
These protections help reduce the amount of untrusted code that can enter a project during dependency installation.
Installing Dependencies Without Running Their Scripts
The simplest way to prevent lifecycle scripts from executing during an npm installation is to use the ignore-scripts option.
Run the following command:
npm install --ignore-scriptsThis instructs npm not to run package lifecycle scripts.
It can be useful when installing unfamiliar dependencies or investigating a package before allowing its installation scripts.
However, there is an important difference between ignore-scripts and npm 12's allowScripts policy.
ignore-scripts disables lifecycle scripts broadly, including scripts required by legitimate packages.
It also prevents preinstall and postinstall hooks from running around explicitly requested npm scripts. The explicitly requested script itself can still run.
For example:
npm run build --ignore-scriptsThe build script can execute, but its automatic prebuild and postbuild hooks are not executed.
For normal development, permanently disabling every dependency installation script may cause compatibility problems.
A better approach is to approve only the packages whose installation scripts have been reviewed.
Managing Trusted Dependencies With allowScripts
npm 12 provides the allowScripts configuration to control which dependencies can execute lifecycle scripts.
This configuration is maintained in the project's package.json file.
For example:
{
"name": "secure-web-app",
"version": "1.0.0",
"dependencies": {
"sharp": "^0.34.0",
"some-library": "^2.0.0"
},
"allowScripts": {
"sharp": true,
"some-library": false
}
}In this example:
sharp is explicitly approved to execute its installation scripts.
some-library is explicitly denied permission to execute its installation scripts.
Dependencies that are not explicitly approved are treated as unapproved.
The important point is that approval should be based on an actual review of the package and its installation requirements.
Do not approve a dependency simply because it is popular or widely used.
Also, approval entries should be maintained carefully when dependencies change versions.
Step 1: Install the Dependencies
Start by installing the project dependencies:
npm installnpm checks the installation policy before executing dependency lifecycle scripts.
Unapproved scripts are skipped, and npm reports packages that require review.
The application dependencies can still be installed, but packages that require installation-time operations may not work correctly until their scripts are approved.
Step 2: Review Packages With Installation Scripts
After installation, inspect the reported dependencies.
You can also use the following command:
npm install-scripts lsThis command lists packages whose installation scripts require attention.
Review the package documentation, package.json file, installation scripts, and the operations performed by the dependency.
For example, a package may need an installation script to download a native binary.
Another package may not require any installation script at all.
Understanding this difference helps developers make better approval decisions.
Step 3: Approve a Trusted Package
After reviewing a package, approve its installation scripts:
npm install-scripts approve sharpnpm updates the allowScripts configuration in package.json.
By default, approvals are version-pinned when npm writes the approval entry.
This is useful because the approval applies to the reviewed package version rather than automatically trusting every future version.
To apply an approval to all future versions by package name, npm also supports disabling approval pinning:
npm install-scripts approve sharp --no-allow-scripts-pinHowever, allowing all future versions increases the trust scope and should be avoided unless there is a clear reason.
Step 4: Deny an Untrusted Package
If a package contains installation scripts that should not execute, explicitly deny them:
npm install-scripts deny some-libraryThis records the denial in the project's allowScripts configuration.
Explicit denials are useful when a dependency is required by the application but its installation scripts are unnecessary or not trusted.
Keep in mind that denying scripts does not remove the package or guarantee that the package itself is safe. Its application code may still execute when imported or used.
Enforcing a Strict Installation Policy
For development teams, warnings may not be enough.
An unapproved dependency script could otherwise be skipped while the installation continues.
npm provides the strict-allow-scripts option to turn unapproved installation scripts into installation failures.
Configure it in the project's .npmrc file:
strict-allow-scripts=trueNow, if an installed dependency contains an installation script that has not been explicitly approved or denied, npm fails the installation.
This makes the approval process more visible and helps prevent developers from accidentally ignoring security warnings.
Explicitly denied dependencies remain blocked without turning their denial into an installation failure.
For production CI/CD pipelines, a strict policy is useful because it prevents an unexpected dependency from silently introducing new installation-time execution requirements.
Integrating npm 12 Security Into CI/CD Pipelines
Dependency installation is particularly important in automated build environments.
CI/CD systems frequently receive credentials that allow access to private repositories, package registries, cloud services, and deployment environments.
A malicious installation script running in such an environment may attempt to access those credentials.
A safer pipeline should use the project's reviewed dependency policy.
For example:
name: Secure Node Build
on:
push:
branches:
- main
jobs:
build:
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@v4
- name: Setup Node.js
uses: actions/setup-node@v4
with:
node-version: 24
cache: npm
- name: Verify npm version
run: npm --version
- name: Install dependencies
run: npm ci
- name: Build application
run: npm run build
- name: Run tests
run: npm testThis workflow uses npm ci to install dependencies from the lockfile.
For a strict npm 12 installation policy, commit the project's reviewed allowScripts configuration and include strict-allow-scripts=true in its .npmrc file.
The CI environment should use the same approved dependency policy as local development.
Also, use a trusted Node.js and npm installation that supports the required npm 12 features.
The workflow should not rely on an unreviewed dependency installation process simply because it runs inside an automated environment.
Advantages and Disadvantages of npm 12 Script Controls
Advantages
Reduced installation-time risk: Unapproved dependency lifecycle scripts are not automatically executed.
Explicit trust decisions: Developers can review packages before allowing their installation scripts.
Version-specific approvals: Approval pinning helps limit trust to reviewed package versions.
Better CI/CD security: Strict installation policies help prevent unexpected scripts from executing during automated builds.
Improved visibility: Developers can identify dependencies that require installation-time execution.
Disadvantages
Additional configuration: Teams must maintain approval policies as dependencies change.
Compatibility issues: Some packages require installation scripts to download or compile necessary components.
More installation steps: Developers may need to review and approve dependencies before completing setup.
Migration effort: Existing projects may need changes to their installation workflows.
False confidence risk: Blocking installation scripts does not guarantee that a dependency is safe.
Common Mistakes Developers Should Avoid
Approving Every Dependency
One of the biggest mistakes is approving every dependency simply to make installation succeed.
Use the following command carefully:
npm install-scripts approve --allThis approves all dependencies with unreviewed installation scripts.
Although it can be useful during a controlled migration, it removes the review step that the security policy is designed to introduce.
Permanently Using ignore-scripts
Using ignore-scripts everywhere may prevent required native modules or generated files from being prepared correctly.
Use it when you intentionally want to disable installation scripts, not as a replacement for a reviewed dependency approval policy.
Assuming the Lockfile Makes Scripts Safe
A package-lock.json file helps reproduce dependency versions, but it does not prove that the package's installation scripts are safe.
Review the package and its lifecycle behavior even when the dependency version is locked.
Trusting Only Direct Dependencies
A project may directly depend on a trusted package that depends on several other packages.
Review the complete dependency tree, not just the packages listed directly in package.json.
Exposing CI/CD Credentials During Installation
Avoid providing unnecessary deployment credentials to dependency installation steps.
Use restricted credentials, short-lived tokens, and separate pipeline stages wherever possible.
Best Practices for Production Applications
Use explicit package approvals. Approve installation scripts only after reviewing their purpose and behavior.
Enable strict installation checks. Configure strict-allow-scripts in team projects and CI environments.
Keep package-lock.json committed. Use npm ci for reproducible dependency installation.
Review dependency changes. Whenever a package is added or updated, inspect whether it introduces new installation scripts.
Pin approvals where possible. Prefer version-specific approvals instead of automatically trusting every future package version.
Restrict CI/CD permissions. Run dependency installation with the minimum permissions required.
Use trusted package registries. Control which registries and package sources are allowed in development and production builds.
Monitor dependency security. Use npm audit and additional software supply chain security checks.
Test native dependencies. Verify that packages requiring native binaries or compilation work correctly when their scripts are explicitly approved.
Document approval decisions. Maintain a clear record of why a dependency requires installation-time execution.
Summary
npm 12 introduces stronger controls for dependency installation by changing how lifecycle scripts are handled.
Instead of automatically running installation scripts from every dependency, developers can use the allowScripts policy to approve trusted packages and block unreviewed or explicitly denied scripts.
The npm install-scripts command makes it easier to review, approve, and deny dependencies. For teams that need stronger enforcement, strict-allow-scripts can turn unapproved installation scripts into installation failures.
However, disabling installation scripts is not a complete dependency security solution. Packages can still execute code when imported, application scripts can still run, and compromised dependencies may introduce other risks.
For production applications, combine npm 12 installation controls with lockfiles, dependency reviews, restricted CI/CD permissions, trusted package sources, and regular security checks.
The goal is not to prevent every dependency from executing necessary installation code. It is to make sure that installation-time execution happens only when the development team has reviewed and explicitly approved it.


Join the conversation! Your thoughts help the community grow.