npm and Package Scripts for React Projects

Learn how npm installs React dependencies, records exact versions in a lockfile, and runs repeatable project scripts with a safer everyday workflow.

published: reading time: 8 min read author: Geek Workbench
Quick Summary

npm installs the dependency tree recorded by a project's manifest and lockfile, then runs its named scripts. This guide uses Node.js 24.22.3 with npm 11.19.1, shows when to use `npm ci`, and separates application packages from build tools. It also covers install scripts, lockfile review, and a clean-checkout build.

npm and Package Scripts for React Projects

Introduction

In a React project, npm installs libraries and runs commands defined by the project. package.json lists packages and scripts; package-lock.json records resolved versions so installs stay consistent. A small package.json might name its local and production commands like this:

{
  "scripts": {
    "dev": "vite",
    "build": "vite build"
  }
}

Then npm run dev starts the local server and npm run build creates the production bundle. The exact scripts depend on the project; npm runs the commands listed in package.json.

Version context

The examples use Node.js 24.22.3 LTS with npm 11.19.1, the npm version bundled with that Node release. Check your installed versions with node --version and npm --version. For a real project, follow its .nvmrc, .node-version, package.json engines field, or CI configuration when those specify a toolchain. A lockfile records dependency resolution; it does not pin Node.js or npm.

The Node.js 24 release archive lists the Node and bundled npm versions.

Install from the lockfile

For day-to-day work, npm install resolves dependencies from package.json and may update package-lock.json. Review both files together when you add or update a package. The manifest usually contains version ranges; the lockfile records resolved versions, including transitive dependencies.

Use npm ci in CI and in a clean-checkout exercise. It requires a lockfile that matches package.json, removes an existing node_modules, and installs the recorded tree without rewriting either package file. It fails when the manifest and lockfile disagree. This makes dependency installation easier to reproduce, but the Node/npm versions and platform still matter.

Install commands can run scripts from the project or its dependencies. npm ci does not disable those hooks: dependencies can run preinstall, install, and postinstall, and some dependency types use prepare. Review unfamiliar packages before installing them, and keep credentials out of the environment when running code you have not vetted. npm audit reports known advisories; it does not certify that a package is safe.

Run project commands with npm run, such as npm run dev, npm run build, or npm run lint, if those scripts exist.

graph TD
    A[Read package.json] --> B[Install from lockfile]
    B --> C[Run a named script]
    C --> D{Change dependencies?}
    D -->|No| E[Build or develop]
    D -->|Yes| F[Review manifest and lockfile]
    F --> E

Before adding a package, check what it does and whether it fits the versions already in use. Install it, review changes to both package files, then run the build and lint commands. npm install zod adds an app dependency; npm install --save-dev vitest adds a development tool. Prefer a targeted update over a broad upgrade, which makes regressions harder to track.

Trade-offs

Choice Useful when Cost
npm install Local setup or changing dependencies May update resolved versions within allowed ranges
npm ci CI and clean reproductions Removes existing node_modules and needs a consistent lockfile
dependencies The app needs a package at runtime, in the browser or on a server Included in production installs
devDependencies A package is only needed to develop, build, test, or check the app Production-only installs may omit it

In a Vite React project, vite and test tools usually belong in devDependencies: Vite builds the files, and the browser runs the output. A library imported by the application and shipped in that output belongs in dependencies. Server-rendered React applications may also have runtime packages on the server. Check how the project deploys before moving a package between sections.

Reproduce a clean build

From the root of a freshly cloned Vite project that commits package-lock.json and defines a build script, run:

node --version  # This example uses v24.22.3
npm --version   # This example uses 11.19.1
npm ci
npm run build

The project should build without changing package.json or package-lock.json. If npm ci fails, inspect the manifest and lockfile diff instead of switching to --force.

Production failure scenarios

If an install works on one machine but not another, check whether the manifest and lockfile changed together, then run npm ci from a clean checkout. It reproduces the committed lockfile and surfaces mismatches.

A peer dependency conflict can stop a CI install after a package update. Inspect the package versions and their peer ranges before considering --force; forcing the install may hide a real incompatibility.

Common pitfalls

  • Editing node_modules by hand; npm recreates it on the next install.
  • Keeping multiple package-manager lockfiles in one repository, which makes the dependency source of truth unclear.
  • Adding a package for a small helper that would be easier to maintain as a short function.

When npm fits

Use npm when the repository already uses it or when you want the standard Node package manager. If a team uses pnpm or Yarn, stay with that manager rather than introducing a second lockfile. Avoid switching package managers mid-project without a migration plan.

Security notes

Check unfamiliar packages because install scripts can run code on your machine. npm audit reports known advisories; review each finding before applying fixes, since an update can break the app. Do not put credentials in package scripts or publish them in registry configuration.

Observability checklist

  • Keep npm ci, build, and lint output in CI logs so install and compile failures are visible.
  • After a dependency-related deploy failure, inspect the lockfile diff and the failing package name.
  • Review audit findings and record which dependency update resolved them.

Quick recap

  • Keep app runtime imports in dependencies and tooling in devDependencies.
  • Commit package.json and package-lock.json together.
  • Use npm ci for repeatable clean installs and npm run for named scripts.
  • Review dependency changes, then run the checks the project provides.

Interview questions

1. Why should a React project commit package-lock.json?
It records the resolved versions, including transitive dependencies. Commit it with package.json so CI and teammates can recreate that dependency tree.
2. What is the difference between npm install and npm ci?
Use npm install when changing dependencies; it may update the lockfile. Use npm ci when CI or a clean checkout should install the committed lockfile as-is.
3. When does a package belong in devDependencies?
Put build, test, lint, and development tooling in devDependencies. If the application needs the package at runtime, put it in dependencies.
4. Why does npm ci fail when the lockfile is out of sync?
npm will not silently choose a new dependency tree during a clean install. Update package.json, regenerate the lockfile with npm install, and review both changes.
5. What should you check before using --force for a peer conflict?
Find which packages disagree and check their supported peer ranges. --force may let installation finish while leaving an incompatibility for build or runtime.
6. What does npm run do?
It runs a script from package.json, such as npm run build. Running npm run without a script lists the commands the project defines.
7. Why should teams avoid multiple lockfiles?
npm, pnpm, and Yarn can resolve different dependency trees. Choose the manager the project uses and commit its lockfile.
8. What should you do with an npm audit finding?
Read the advisory and inspect the proposed package change. A fix may require a version update that affects compatibility, so run the project's build and checks afterward.
9. Why can package installation itself carry risk?
Install hooks run code in the environment doing the install. Check unfamiliar packages before use, and do not expose credentials to code you have not vetted.
10. When should you use npm install instead of npm ci?
Use npm install for local setup or to add and update packages. It can change the lockfile; use npm ci to install the committed versions from a clean state.

Further reading

Conclusion

Keep the manifest and lockfile in the same change. Use npm install when changing dependencies, review the resulting diff, and use npm ci to reproduce the committed dependency tree in CI or from a clean checkout.

Category

Related Posts

DOM, HTML, and CSS for React Interface Basics

Understand how semantic HTML, the browser DOM, CSS layout, and React updates fit together when building accessible, predictable interfaces.

#react #html #css

Modern JavaScript Modules for React Projects

Learn how JavaScript modules organize React code with named and default exports, practical import patterns, common failure fixes, and build boundary tips.

#javascript #esm #modules

Accessible React UI: Semantics, Labels, and Keyboard Use

Build React interfaces with semantic controls, connected labels, visible keyboard focus, and status announcements through practical TypeScript examples.

#react #typescript #accessibility