You get every Node module back by reinstalling from your project's lockfile (npm ci for npm, or pnpm install --frozen-lockfile/yarn install --immutable), which rebuilds node_modules to the exact versions recorded when dependencies last resolved. If the lockfile is missing or stale, run a clean install (rm -rf node_modules package-lock.json && npm install) so npm re-resolves the full tree from package.json. The condition that matters: your manifest must list every dependency you actually import, because no reinstall can bring back a package that was never declared.

That distinction—reinstall versus re-declare—is where most "lost modules" stories go wrong. People delete the folder, run install, and still see Cannot find module 'x', then conclude npm is broken. Usually the package was installed ad hoc, hoisted into a workspace unexpectedly, or pruned by a partial command. The steps below walk through recovery in the order that preserves the most information, then explain how to diagnose the cases where a plain reinstall isn't enough.

What "Getting Every Module Back" Actually Means

Node's module resolution doesn't keep a central registry of "modules your app had." The node_modules folder is a materialized view of decisions recorded in two files plus whatever commands you ran. package.json declares direct dependencies with version ranges; the lockfile pins the fully resolved tree, including transitive packages, integrity hashes, and peer-dependency outcomes. Deleting the folder destroys only the materialization, not the record—provided the record still exists.

Two categories of loss follow from that. Recoverable loss means node_modules was deleted, corrupted, or partially installed while package.json and the lockfile survive; a correct install restores everything. Non-recoverable-by-install loss means a package was never saved to package.json, the lockfile was regenerated under different conditions, or the package came from a non-registry source (a global install, a sibling folder via file:, a private registry you can no longer reach) whose address isn't recorded correctly. Install commands cannot resurrect what was never written down.

One more nuance: "every module" can mean every direct dependency or literally every package in the tree, including transitive ones. Lockfile-based installs restore the entire tree, so both readings converge—unless you regenerate the lockfile, in which case transitive versions may drift even as direct dependencies stay pinned loosely.

Prerequisites Before You Touch Anything

Before running a single command, gather four things. First, confirm which package manager the project uses: a package-lock.json indicates npm, pnpm-lock.yaml indicates pnpm, yarn.lock indicates Yarn, and bun.lock or bun.lockb indicates Bun. Mixing managers on one project produces duplicate trees and confusing resolution. Second, know your Node version and whether the project pins one (.nvmrc, the engines field, or Volta settings); native modules compiled against a different ABI will fail at runtime even when installed correctly. Third, commit or stash pending work—clean installs delete node_modules wholesale, and any patched package inside it (via patch-package output applied at install time, or manual edits) disappears with it. Fourth, note whether the project is a monorepo with workspaces, because installs must run from the workspace root, not a package subfolder.

Check that package.json exists and contains the dependencies you expect. If it's gone too, recovery shifts from reinstall to reconstruction—restore it from version control first.

The Ordered Recovery Procedure

Step 1: Verify the lockfile exists and matches the manifest. Compare the lockfile's packages[""].dependencies (npm v7+ format) or top-level entries against package.json. A lockfile whose recorded ranges no longer satisfy package.json will be updated rather than honored during install, which is acceptable but means versions may move. If both files are present and consistent, continue.

Step 2: Delete the current tree. Remove node_modules entirely rather than trying to repair it in place. On macOS or Linux: rm -rf node_modules. On Windows PowerShell: Remove-Item -Recurse -Force node_modules. In a workspace monorepo, remove the root node_modules and any per-package ones. Partial deletion is the most common cause of the "module not found" error persisting after reinstall, because stale nested copies shadow fresh ones.

Step 3: Clear the package manager's cache only if installs keep failing. Cached tarballs are usually fine and clearing them costs download time. Reserve npm cache verify (safe, checks integrity) for suspected corruption, and treat npm cache clean --force as a last resort. On pnpm, pnpm store prune removes unreferenced packages without harming your project.

Step 4: Reinstall from the lockfile. Run npm ci. It deletes node_modules itself, refuses to proceed if the lockfile disagrees with package.json, and installs the exact locked tree. For pnpm: pnpm install --frozen-lockfile. For Yarn Berry: yarn install --immutable. For Bun: bun install --frozen-lockfile. This is the step that "gets every module back" in the recoverable case.

Step 5: Rebuild native modules if needed. Packages with compiled components (database drivers, image processors, some crypto libraries) either ship prebuilt binaries or compile during install. After a Node major-version change, run npm rebuild or simply reinstall under the correct Node version. If install scripts were skipped (npm's --ignore-scripts, or a CI setting), those modules will exist but fail on first use—rerun the install with scripts enabled.

Step 6: Verify. Run your test suite or at minimum start the app and exercise the import paths. A quick structural check: npm ls --all prints the resolved tree and flags missing or invalid entries; npm ls --depth=0 shows direct dependencies only. Expect exit code 0 and no "UNMET DEPENDENCY" lines. Then run one build step, since bundlers resolve differently from Node and surface missing peer dependencies.

If you have no lockfile at all, replace step 4 with a resolving install: npm install (or pnpm install/yarn install). It reads package.json, fetches the newest versions satisfying each range, and writes a new lockfile. Everything declared returns, but versions may differ from what you had.

When You Have No Lockfile or Manifest

If node_modules and the lockfile are both gone but package.json remains, npm install regenerates a complete tree—subject to version drift and to any dependency whose registry metadata changed. If package.json is also gone, the recovery path narrows. Restore from git (git checkout HEAD -- package.json package-lock.json), from a backup, or from a sibling checkout or Docker image that still has the files. Failing that, reconstruct the manifest from imports: search your source for require('…') and from '…' statements, filter out Node built-ins and relative paths, and add the remaining package names. You can list what a surviving folder contained with npm ls --depth=0 if any node_modules remains, or read package.json out of a shipped artifact's metadata.

A subtler case: the lockfile exists but was generated under a different registry or platform. Lockfiles record resolved URLs and, for native packages, platform-specific optional dependencies. A lockfile built on macOS may omit Linux-only binaries; installing it on Linux with npm ci can leave a native module absent. The fix is a normal resolving install on the target platform, which fills in the optional dependencies, then committing the updated lockfile.

How Modules Go Missing in the First Place

Understanding the cause tells you whether reinstall will work. Accidental deletion—cleaning scripts, disk tools, git clean -fdx that removes ignored folders—is fully recoverable from the lockfile. Interrupted installs leave half-written trees; this is why npm ci deletes before installing rather than patching. Global installs are a classic trap: npm install -g some-cli puts the package in a global prefix, not your project, so require('some-cli') was never going to resolve from local code. The remedy is a local install plus npx or a scripts entry, not a reinstall.

Hoisting surprises account for many monorepo mysteries. With workspaces, a package your code imports may be installed at the root because a sibling package depends on it. It resolves today, vanishes when the sibling drops that dependency, and reappears only if you declare it yourself. The durable fix is adding it to your own package.json. Peer dependencies behave similarly: npm 7+ auto-installs peers, older setups and some pnpm configurations don't, so a plugin can work on one machine and not another. Also check version mismatches—two copies of a package at incompatible major versions produce runtime errors that look like missing modules but are really resolution conflicts, which npm ls will show as a duplicated or invalid tree.

Choosing the Right Command for Your Situation

Situation Command Effect on versions
Lockfile present, manifest unchanged npm ci Exact locked versions; fails on mismatch
Lockfile present, manifest edited npm install Updates lockfile to satisfy new ranges
No lockfile npm install Resolves newest satisfying versions
pnpm project, lockfile trusted pnpm install --frozen-lockfile Exact versions; fails on mismatch
Yarn Berry project yarn install --immutable Exact versions; fails on drift
Suspected cache corruption npm cache verify, then npm ci Unchanged
Node major version changed npm ci then npm rebuild Unchanged; recompiles natives

The comparison criterion is reproducibility: lockfile-strict commands guarantee the same tree across machines and CI, while resolving installs guarantee coverage at the cost of drift. Pick strict when the lockfile is trustworthy and resolving when it isn't—or when platform-specific optional dependencies must be filled in.

What Reinstalling Cannot Fix

Three failure classes survive every install command. Unreachable sources: a dependency pointing at a private registry, a git URL, or a file: path that no longer exists will fail to fetch regardless of lockfile fidelity—the error names the URL, and you must restore access or repoint the entry. Deleted upstream versions: if a lockfile pins a version that was unpublished (rare but real on some registries), installs fail with a 404 on that tarball; the fix is editing the lockfile to a nearby published version, which changes your tree. Runtime-only resolution issues: a package installed correctly but imported with the wrong specifier (case sensitivity on Linux versus macOS, a missing subpath export in the package's exports map, or an ESM/CJS mismatch) throws "Cannot find module" while the folder sits right there. That's a code or package-configuration problem, not a dependency-loss problem, and reinstalling repeatedly won't touch it.

A stop condition worth knowing: if npm ci fails on integrity-hash mismatches for multiple unrelated packages, or if the same private-registry authentication error persists after refreshing your token, stop and involve whoever administers that registry. Continuing to delete and reinstall risks overwriting the lockfile with a subtly different tree and losing the record you're trying to restore.

Preventing the Next Loss

Commit lockfiles—always, for applications. Keep node_modules out of version control but treat the lockfile as source. Run npm ci (not npm install) in CI so drift fails loudly rather than silently. Avoid global installs for anything your code imports; use npx for one-off tools and devDependencies for build tooling. In monorepos, declare every package each workspace imports directly, even if hoisting makes it seem unnecessary. And if you patch packages, record the patches in a repeatable mechanism so a reinstall reapplies them instead of quietly dropping them.

Follow those habits and "getting every module back" collapses to one strict install from a committed lockfile—predictable, fast, and complete.