Many developers run npm install almost reflexively: entering the project folder, typing the command, and then waiting for node_modules to finish building. This habit is practical, but one thing is often overlooked: the installation process can execute code from the installed packages.
This code is typically used for legitimate purposes, such as compiling native modules, setting up binaries, or running build processes. However, from a security perspective, any code running on your computer should still be treated as something that must be trusted. npm supports lifecycle scripts like preinstall, install, postinstall, and prepare that can be triggered at certain stages during installation. ([docs.npmjs.com](https://docs.npmjs.com/using-npm/scripts.html/?utm_source=openai))
The Issue Is Not Just the Main Package
When you install one package, the project can pull in many other dependencies. The main package visible in package.json may only depend on a few modules, but those modules can have further dependencies.
This is why the package name alone is not enough to assess risk. What needs to be considered is the entire dependency tree: who is installed, their versions, where they come from, and whether any additional processes are run during installation.
A simple analogy is like receiving a box containing electronic devices. You don't just receive the main device, but also cables, adapters, and small components from various suppliers. Most may be safe and necessary, but you still want to know what is coming into your home.
Lockfile Is Not Just Trash
Files like package-lock.json are often seen as byproducts that can be deleted. In fact, the lockfile records the dependency tree more specifically so that team members, deployment servers, and continuous integration pipelines can install the same versions. npm also documents the lockfile as a file that should be kept in the source repository. ([docs.npmjs.com](https://docs.npmjs.com/files/package-lock.json.html/?utm_source=openai))
Without a lockfile, two people can run the same command at different times but obtain different versions of dependencies. These small differences can lead to bugs, behavioral changes, or variations in security risks that are hard to trace.
For consistent installations in build or deployment environments, use:
npm ciThis command is designed to work from the lockfile and is usually more suitable for automated environments. On development machines, npm install remains useful when you are indeed adding or updating dependencies.
Check Scripts Before Trusting Packages
Before adding a new package, open its package.json and look at the scripts section. Pay special attention to scripts related to installation:
{
"scripts": {
"preinstall": "...",
"install": "...",
"postinstall": "...",
"prepare": "..."
}
}The presence of scripts is not proof that the package is dangerous. Modules that require native compilation may indeed need them. However, scripts that download executables from unclear locations, read many environment variables, or run hard-to-understand shell commands deserve further scrutiny.
If you are testing an unfamiliar package, you can disable lifecycle scripts with:
npm install --ignore-scriptsThe ignore-scripts configuration makes npm not run scripts defined in the package.json file. Keep in mind that some packages may not function fully without those installation processes. So, this option is not an absolute rule for all projects, but rather a risk-reduction tool during initial checks. ([docs.npmjs.com](https://docs.npmjs.com/cli/using-npm/config/?utm_source=openai))
Do Not Assume npm Audit Is a Safety Guarantee
After dependencies are installed, run:
npm auditThis command checks the project's dependencies against known vulnerability advisories and may suggest fixes. npm also provides npm audit fix, but this command can change the dependency tree because it runs installation processes to apply remediation. ([docs.npmjs.com](https://docs.npmjs.com/cli/audit/?utm_source=openai))
The audit results are not a certificate that the project is safe. Audits primarily help identify known vulnerabilities. They do not automatically answer questions like: is the package well-maintained, can the maintainer be trusted, or does a script do something unnecessary for the application.
Therefore, do not just run npm audit fix --force to clear the warning list. Major changes to dependency versions can introduce new compatibility issues. Read the affected packages, check their dependency paths, and test changes in a separate branch.
A Simpler and Safer Workflow
- Ensure the package source is clear. Check the package name, documentation, repository, maintenance activity, and whether the package is truly needed.
- Install in a separate branch or environment. Do not test foreign packages directly on the production server.
- Check lifecycle scripts. Look for
preinstall,install,postinstall, andpreparebefore running the usual installation. - Use lockfiles. Commit
package-lock.jsonand usenpm ciin deployments that require consistent results. - Run audits regularly. Treat the results as a to-do list, not as a security stamp.
- Review lockfile changes. If one line in
package.jsonadds dozens of packages, understand why before merging.
What Does This Mean for Us?
Dependency security does not mean stopping the use of open source or manually checking every line of code. The goal is to make more informed decisions. Small, unmaintained packages can carry more burdens than they appear, while popular packages can still have issues in certain versions.
The most useful habit is to separate three questions: what am I installing?, what code is running when I install it?, and how do I reproduce the same installation later? The lockfile helps answer the third question. Checking scripts helps answer the second question. Audits and reviewing package sources help answer the first question.
With these steps, npm install remains a practical tool without being treated as a magic button that is always safe.
Sources & Further Reading
- npm Docs: Scripts and lifecycle events
- npm Docs: package-lock.json
- npm Docs: npm audit
- npm Docs: npm configuration and ignore-scripts
- npm Docs: npm ci
– Rio Yotto @rioyotto
