Skip to content
SecurityChapter 39 / 57Advanced5 min read

Dependencies & Supply Chain

The basics of npm supply chain security. Use a lockfile and npm ci, automate updates, choose new packages carefully and keep your CI workflows safe.

On this page

1. What is this practice?

Supply chain means everything that reaches your code from outside: npm packages, their dependencies, tools downloaded in CI, and GitHub Actions. If malicious code gets into any one of them, it reaches your server and your users.

2. Why does it matter?

Even if a typical Next.js project has 30 direct dependencies, node_modules holds hundreds of packages. The maintainer of each one can run code in your production.

Without disciplineWith discipline
A new version can arrive on every installThe lockfile fixes exact versions
npm install in CI can change the lockfilenpm ci in CI installs strictly from the lockfile
Updates once a year, and everything breaksSmall grouped updates every week
A peer conflict hidden with a legacy flagThe conflict is resolved
A workflow with full write permissionMinimum permissions

3. When to follow it

LevelHow importantWhy
Learning projectCommit the lockfile and build the habit of choosing packagesA typosquatted package can do damage even on your laptop
Production codeThe full rule, with automated updatesA compromised package reaches your users' data
Senior levelCI hardening, pinned tools, review processThe pipeline itself is an attack surface

4. Bad vs good code

Bad

bash
npm install some-pakage
git add package.json
git commit -m "add lib"
json
{
  "dependencies": {
    "date-lib": "*",
    "ui-kit": "latest"
  }
}

In CI:

bash
npm install --legacy-peer-deps

A spelling mistake (some-pakage) opens the door to typosquatting, the lockfile was not committed, and * or latest means whatever is newest on every build.

Good

bash
npm view some-package maintainers time.modified repository.url
npm install some-package
git add package.json package-lock.json
git commit -m "add some-package for csv parsing"

In CI:

bash
npm ci
npm audit --audit-level=high

Minimum permissions in a GitHub Actions workflow:

yaml
on:
  pull_request:

permissions:
  contents: read

jobs:
  test:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - uses: actions/setup-node@v4
        with:
          node-version-file: .nvmrc
      - run: npm ci
      - run: npm test

What is the difference?

ThingBadGood
Version* or latestExact in the lockfile
CI installnpm installnpm ci
Peer conflict--legacy-peer-depsResolve the real conflict
New packageWithout lookingCheck maintainers, activity, repo
WorkflowDefault permissionscontents: read

5. How to apply it

Before adding a new package
Can the platform or an existing package do this job?
Confirm the spelling of the name and the official repo
Look at the maintainers, last publish and open issues
Look at install scripts and the dependency count
Install it and commit it together with the lockfile

At the repo level:

JobHow
Automated updatesDependabot or Renovate on, one grouped PR per week
Auditnpm audit in CI, fail on high and critical
CI toolsPin the version of downloaded binaries and verify the checksum
PR workflowsUse pull_request, not pull_request_target, when you need to run the PR's code
Third party scriptsPin vendored scripts in the repo by hash and review them like code

pull_request_target runs with the base repo's secrets. If it runs code from a PR that came from a fork, the attacker gets your secrets.

6. Exceptions and common mistakes

When the rule does not apply: a small throwaway script that is never deployed does not need this much process. But running a random package with npx also runs code on your machine, so check the name there too.

Mistake: Clearing an audit warning with npm audit fix --force

Why? Force changes major versions and the app can quietly break.

Better: Read the report, check whether you actually use the vulnerable code path, then do a controlled upgrade.

Mistake: Not merging updates for months

Why? Then one day you have to upgrade ten major versions at once for a security fix.

Better: Merge the weekly grouped update PRs the same week once CI is green.

Mistake: A big package for a small job

Why? Every package brings its own dependencies and maintainers with it.

Better: Write a two or three line job yourself or use a platform API, like structuredClone or Intl.

Mistake: A deprecated package for security work

Why? Bugs in an unmaintained crypto or auth library will never be fixed.

Better: Use the language or framework's built-in or an actively maintained library.

7. What to learn before and after

Final quick revision

CheckYes?
package-lock.json committed
npm ci in CI
Dependabot or Renovate, weekly and grouped
No --legacy-peer-deps in CI
Minimum permissions in workflows
Checked before adding a new package