Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Next.js and React

Your project has a package.json but no node_modules folder

Errornode_modules does not exist after cloning the project

Short answer

node_modules only exists after npm installs the dependencies. If the folder is missing, install has not run in this folder or it failed. If only dev tools are missing, NODE_ENV=production told npm to skip devDependencies. Run npm install in the folder that holds package.json, or npm ci when a lockfile exists.

  • Applies to npm 10, Node.js 22
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Check that you are in the folder that has package.json, whether node_modules exists there, and whether NODE_ENV is set. The output points at the cause below.

    pwd && ls -a
    echo "NODE_ENV=$NODE_ENV"
    npm config get omit
  2. 2Match what you see, then jump to its fix

    1. 1No node_modules in ls -a, for example right after a git clone or a downloadThe install was never run in this folderFix
    2. 2node_modules exists, but tools such as react-scripts, vite or typescript are not foundNODE_ENV=production makes npm skip devDependenciesFix
    3. 3npm ci exits with an error saying package.json and the lockfile do not match, and installs nothingnpm ci refuses because the lockfile and package.json differFix

What does the error look like?

  • node_modules does not exist after cloning the project
  • Dev tools such as react-scripts or tsc are missing although node_modules exists

Cause 1 of 3

The install was never run in this folder

1Check

node_modules is not stored in git, so a fresh copy of a project never has it. Confirm you are in the same folder as package.json.

ls package.json package-lock.json node_modules

2Fix

Run the install. With no arguments, npm installs the dependencies listed in package.json into node_modules.

npm install

3Verify

The folder appears and the project command runs.

ls node_modules | head
npm run build

Confirmed by docs.npmjs.com

Did this fix it?

Cause 2 of 3

NODE_ENV=production makes npm skip devDependencies

1Check

npm's omit setting defaults to 'dev' when NODE_ENV is production, so build tools listed under devDependencies are resolved but not placed on disk.

echo "$NODE_ENV"
npm config get omit

2Fix

Install with the dev dependencies included, or unset NODE_ENV for the install step and set it again for the build or run.

npm install --include=dev
# or, for this shell only:
unset NODE_ENV && npm install

3Verify

The tool now resolves from the project.

npx --no-install tsc --version

Confirmed by docs.npmjs.com

Did this fix it?

Cause 3 of 3

npm ci refuses because the lockfile and package.json differ

1Check

npm ci never rewrites the lockfile. If package.json was edited by hand, or the lockfile is missing, it exits with an error and nothing is installed.

ls package-lock.json && git status --short package.json package-lock.json

2Fix

Run npm install once to bring the lockfile up to date, commit both files, then use npm ci in automation.

npm install
git add package.json package-lock.json

3Verify

A clean install from the lockfile now works.

npm ci

Confirmed by docs.npmjs.com

Did this fix it?

How do I stop it coming back?

  • Keep node_modules out of git and put the install command in the README for the next person.
  • In CI and Docker use npm ci with a committed package-lock.json.
  • Do not set NODE_ENV=production globally on a machine you also build on; set it only for the run or the final image stage.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.