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.
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_modulesby 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
dependenciesand tooling indevDependencies. - Commit
package.jsonandpackage-lock.jsontogether. - Use
npm cifor repeatable clean installs andnpm runfor named scripts. - Review dependency changes, then run the checks the project provides.
Interview questions
package.json so CI and teammates can recreate that dependency tree.
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.
devDependencies. If the application needs the package at runtime, put it in dependencies.
package.json, regenerate the lockfile with npm install, and review both changes.
--force may let installation finish while leaving an incompatibility for build or runtime.
package.json, such as npm run build. Running npm run without a script lists the commands the project defines.
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
- For the module and import side of this setup, read Modern JavaScript Modules.
- The Vite development workflow shows how project scripts connect to local development and production builds.
- The production build and deployment guide follows the build output to deployment.
- Once the toolchain is in place, TypeScript inference and annotations covers typed React code.
- For clean installs in automation, see the official npm ci documentation.
- For advisory review, use the official npm audit documentation.
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.
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.
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.