Maaz Zindani

Loading the index…

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

TypeScript cannot find '@angular/platform-browser' in an Angular project

Errorerror 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.

  • Applies to Angular 20, Angular 21, Node.js 22, TypeScript 5.9, 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 for this project. Run it in the folder that holds package.json, not in a parent folder.

    npm ls @angular/platform-browser @angular/core
  2. 2Match what you see, then jump to its fix

    1. 1npm 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. 2npm 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. 3The 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

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.json

2Fix

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 ci

3Verify

npm ls lists the package without a problem marker, and the build runs.

npm ls @angular/platform-browser
npx ng build

Confirmed by docs.npmjs.com

Did this fix it?

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/cli

2Fix

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/core

3Verify

All Angular packages now list the same major version.

npm ls @angular/core @angular/platform-browser

Confirmed by angular.dev

Did this fix it?

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.json

2Fix

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.json

Confirmed by typescriptlang.org

Did this fix it?

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:

Related fixes

Back 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 fix

Back 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 fix

Next step

Want this handled for good?

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