TypeScript cannot find module '@nestjs/core' or its type declarations
Cannot 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.
Fix it fast
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" | head2Match what you see, then jump to its fix
- 1
npm ls prints (empty) or the folder node_modules/@nestjs does not existDependencies are not installed in this folderFix - 2
The 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
The package is installed and npm ls finds it, but TypeScript still reports TS2307tsconfig module resolution or paths hides the packageFix
- 1
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.json2Fix
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/common3Verify
npm finds the package and the compiler stops reporting TS2307.
npm ls @nestjs/core
npx tsc --noEmitConfirmed by docs.npmjs.com
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 build3Verify
The build completes without TS2307.
npm run buildConfirmed by typescriptlang.org
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.json2Fix
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 --noEmitConfirmed by typescriptlang.org
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:
- TaxiDexMy own multi-tenant taxi dispatch SaaS: API, dispatcher portal, driver and passenger apps, and the servers underneath.
- QistLockA NestJS and React SaaS driving an Android Device Owner agent that locks financed phones when payments lapse.
- WorkforceA field app, an admin console and a self-hosted Node.js server for push and reconciliation.
Related fixes
Back end
NestJS app will not start: Cannot find module '/app/dist/main'
Cannot find module 'dist/main'
Node cannot find dist/main.js because the build did not put it there.
4 causes, each with a fixBack end
TypeError: something.default is not a constructor in a NestJS app
Exception has occurred: TypeError: xxx.default is not a constructor
TypeScript turns import X from 'pkg' into require('pkg').default when esModuleInterop is off, and a CommonJS package has no default, so new X() fails.
2 causes, each with a fixBack end
TypeScript cannot find req.cookies in a NestJS controller
error TS2339: Property 'cookies' does not exist on type
TypeScript says 'cookies' is missing because the Request type in your file is not Express's, or because cookie support is not installed.
3 causes, each with a fixBack end
TypeORM warns that createConnection is deprecated
'createConnection' is deprecated.
TypeORM replaced Connection with DataSource.
2 causes, each with a fixBack end
TypeORM stops with Duplicate migrations on run or generate
Error: Duplicate migrations: User1655123435373
TypeORM collects every migration class that your migrations glob matches and refuses to continue when two of them have the same class name.
2 causes, each with a fixBack end
TypeORM cannot find an entity, or the metadata for a relation
Entity metadata for Role#users was not found.
TypeORM only knows the entities listed in the entities option of the DataSource that is running.
2 causes, each with a fix