Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Node.js, PHP and APIs

TypeScript cannot find module '@nestjs/core' or its type declarations

ErrorCannot find module '@nestjs/core' or its corresponding type declarations.

Short answer

TS2307 means TypeScript searched node_modules and found neither the package nor its types. For @nestjs/core the usual cause is that dependencies are not installed in the folder you are building from. If they are, check the folder you run in, and the module resolution settings in tsconfig.json.

  • Applies to TypeScript 5.x, NestJS 11, Node.js 22, npm 10
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Ask npm whether the package is installed where you are standing, and ask TypeScript how it resolves it.

    npm ls @nestjs/core
    npx tsc --noEmit --traceResolution | grep "@nestjs/core" | head
  2. 2Match what you see, then jump to its fix

    1. 1npm ls prints (empty) or the folder node_modules/@nestjs does not existDependencies are not installed in this folderFix
    2. 2The package is installed in another folder, such as a monorepo root or a sibling appYou build from a folder that does not contain node_modulesFix
    3. 3The package is installed and npm ls finds it, but TypeScript still reports TS2307tsconfig module resolution or paths hides the packageFix

What does the error look like?

  • Cannot find module '@nestjs/core' or its corresponding type declarations.
  • error TS2307: Cannot find module '@nestjs/common' or its corresponding type declarations.

Cause 1 of 3

Dependencies are not installed in this folder

1Check

After a fresh clone, a deleted node_modules, or an install that stopped early, the package is simply missing. Also confirm @nestjs/core is in package.json dependencies.

ls node_modules/@nestjs
grep -n "@nestjs/core" package.json

2Fix

Run an install in the project root. If the package is not listed in package.json, add it first.

npm install
# only if missing from package.json:
npm install @nestjs/core @nestjs/common

3Verify

npm finds the package and the compiler stops reporting TS2307.

npm ls @nestjs/core
npx tsc --noEmit

Confirmed by docs.npmjs.com

Did this fix it?

Cause 2 of 3

You build from a folder that does not contain node_modules

1Check

For a bare import, TypeScript looks in node_modules next to the importing file and in each ancestor folder. A project outside that chain, or a build run from the wrong directory, never sees the package.

pwd
npx tsc --noEmit --traceResolution | grep -m3 "node_modules"

2Fix

Run the command from the package folder that has the dependencies, or install the dependencies in the app you are building.

cd path/to/nest-app
npm install
npm run build

3Verify

The build completes without TS2307.

npm run build

Confirmed by typescriptlang.org

Did this fix it?

Cause 3 of 3

tsconfig module resolution or paths hides the package

1Check

The old node10 resolution ignores a package's exports field, and a paths entry that points into node_modules bypasses exports and types. Check both settings.

grep -n "moduleResolution\|\"module\"\|paths" tsconfig.json

2Fix

Use a moduleResolution that matches how the project runs (node16 or nodenext for Node.js), and remove paths entries that point at node_modules.

{
  "compilerOptions": {
    "module": "nodenext",
    "moduleResolution": "nodenext"
  }
}

3Verify

Compile again.

npx tsc --noEmit

Confirmed by typescriptlang.org

Did this fix it?

How do I stop it coming back?

  • Run npm ci in CI and in Docker builds, so a missing dependency fails there and not on a teammate's machine.
  • Keep one package.json per app and run all commands from that folder.
  • Do not mix hand-edited moduleResolution settings with the ones the Nest starter generated.

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.