TypeScript cannot find '@angular/platform-browser' in an Angular project
error TS2307: Cannot find module '@angular/platform-browser' or its corresponding type declarations.Short answer
TS2307 means TypeScript could not turn the import into a file. For an Angular package that is one of three things: node_modules is missing or incomplete, the package is not at the same version as @angular/core, or tsconfig sets a module resolution mode that does not read node_modules. Run npm ls first.
Fix it fast
1Find out which cause you have
Ask npm whether the package is installed for this project. Run it in the folder that holds package.json, not in a parent folder.
npm ls @angular/platform-browser @angular/core2Match what you see, then jump to its fix
- 1
npm ls reports the package as missing, or the node_modules folder does not exist after a fresh clone or branch switchnode_modules is missing or was installed incompletelyFix - 2
npm ls prints invalid or a version different from @angular/core@angular/platform-browser is not at the same version as the rest of AngularFix - 3
The package exists on disk and npm ls is clean, but TS2307 stays in the editor or in tsctsconfig.json uses a module resolution mode that does not read node_modulesFix
- 1
What does the error look like?
error TS2307: Cannot find module '@angular/platform-browser' or its corresponding type declarations.Cannot find module '@angular/platform-browser' or its corresponding type declarations.
Cause 1 of 3
node_modules is missing or was installed incompletely
1Check
List the package folder on disk. If the command finds nothing, the install never ran or was cut short.
ls node_modules/@angular/platform-browser/package.json2Fix
Do a clean install from the lock file. npm ci deletes any existing node_modules first and installs exactly what package-lock.json says.
npm ci3Verify
npm ls lists the package without a problem marker, and the build runs.
npm ls @angular/platform-browser
npx ng buildConfirmed by docs.npmjs.com
Cause 2 of 3
@angular/platform-browser is not at the same version as the rest of Angular
1Check
Compare the versions of the Angular packages that must move together.
npm ls @angular/core @angular/platform-browser @angular/compiler @angular/cli2Fix
Let the CLI move all Angular packages together instead of editing one version by hand. ng update also runs the migrations for that release.
ng update @angular/cli @angular/core3Verify
All Angular packages now list the same major version.
npm ls @angular/core @angular/platform-browserConfirmed by angular.dev
Cause 3 of 3
tsconfig.json uses a module resolution mode that does not read node_modules
1Check
When moduleResolution is not set, TypeScript picks a default from the module option, and for several module values that default is classic. The TypeScript docs say not to use classic. Look at both options.
grep -n -E '"(module|moduleResolution)"' tsconfig.json2Fix
Set a mode that reads node_modules. For code built by the Angular CLI, which uses a bundler, the documented pairing is module preserve with moduleResolution bundler.
{
"compilerOptions": {
"module": "preserve",
"moduleResolution": "bundler"
}
}3Verify
Restart the TypeScript server in the editor, then type-check from the terminal.
npx tsc --noEmit -p tsconfig.jsonConfirmed by typescriptlang.org
How do I stop it coming back?
- Commit package-lock.json and use npm ci in CI and in Docker builds.
- Upgrade Angular with ng update, never by changing one @angular package in package.json.
- Open the editor on the folder that holds package.json and tsconfig.json.
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
Angular build fails with TS2707 or "has no exported member" after installing a library
Module '"@angular/core"' has no exported member
These errors mean a library was compiled by a newer Angular than your app uses.
3 causes, each with a fixBack end
cdk synth says --app is required
--app is required either in command-line, in cdk.json or in ~/.cdk.json
The CDK CLI needs a command that runs your app and finds it in three places: the --app option, cdk.json in the project folder, or cdk.json in your home folder.
3 causes, each with a fixBack end
TypeScript cannot find module '@nestjs/core' or its type declarations
error TS2307: Cannot find module '@nestjs/common' or its corresponding type declarations.
TS2307 means TypeScript searched node_modules and found neither the package nor its types.
3 causes, each with a fixBack end
Mocha or Node cannot find ts-node/register
Error: Cannot find module 'ts-node/register'
Node resolves ts-node/register from your project's node_modules, so the package must be installed there, next to typescript.
2 causes, each with a fixBack end
TypeError: Class constructor cannot be invoked without 'new' in compiled TypeScript
TypeError: Class constructors cannot be invoked without 'new'
Node.js throws this when a native class is called like a plain function.
2 causes, each with a fixBack end
create-next-app keeps creating a TypeScript project
Would you like to use TypeScript? No / Yes
create-next-app now makes a TypeScript project by default.
2 causes, each with a fix