Maaz Zindani

Loading the index…

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

tsc reports type errors in files under node_modules

Errornode_modules/@types/some-package/index.d.ts(12,5): error TS2300: Duplicate identifier

Short answer

tsc type-checks every declaration file it loads, including the .d.ts files of your dependencies. Set skipLibCheck to true to skip checking them, and set an explicit types list so tsc stops loading every @types package it finds. Your own code is still fully checked.

  • Applies to TypeScript 5.9, Node.js 22
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    List which files the errors come from. If every path starts with node_modules, this page applies.

    npx tsc --noEmit | grep -c node_modules
  2. 2Match what you see, then jump to its fix

    1. 1Errors in node_modules/**/*.d.ts that you did not write and cannot editDeclaration files of dependencies are being type-checkedFix
    2. 2Errors come from @types packages your project never uses, such as test or tooling typings pulled in by another packageEvery installed @types package is pulled into the buildFix

What does the error look like?

  • node_modules/@types/some-package/index.d.ts(12,5): error TS2300: Duplicate identifier
  • error TS2688: Cannot find type definition file for '...'

Cause 1 of 2

Declaration files of dependencies are being type-checked

1Check

By default tsc also checks declaration files. A package whose typings need a newer TypeScript, or two typings that clash, then fail your build for reasons outside your code.

npx tsc --version && npm ls typescript

2Fix

Turn on skipLibCheck. It skips type checking of declaration files, at the cost of some accuracy in the type system.

{
  "compilerOptions": {
    "skipLibCheck": true
  }
}

3Verify

Run the type check again; errors from node_modules should be gone.

npx tsc --noEmit

Confirmed by typescriptlang.org

Did this fix it?

Cause 2 of 2

Every installed @types package is pulled into the build

1Check

Before TypeScript 6.0, when types is not set, all visible @types packages are included. That drags in typings you never asked for.

ls node_modules/@types

2Fix

List only the typings you need. When types is set, only those packages go into the global scope.

{
  "compilerOptions": {
    "types": ["node"]
  }
}

3Verify

Type-check again.

npx tsc --noEmit

Confirmed by typescriptlang.org

Did this fix it?

How do I stop it coming back?

  • Set skipLibCheck and types in the first tsconfig of a project.
  • Keep one typescript version in the tree; check it with npm ls typescript after upgrades.
  • Update @types packages together with the packages they describe.

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.