TypeScript: error TS2688: Cannot find type definition file for...random paths.

Using https://github.com/atrauzzi/gerty on the branch hashi-gerty.

Basically anything that tries to do typescript gets a bunch of errors about not finding type definitions I never reference in any of my source files.

(For the simplest example, I do a yarn install and then ./node_modules/.bin/ts-node.)

PS C:\Users\atrauzzi\Development\atrauzzi\gerty> .\node_modules\.bin\ts-node
> console.log('hi');
error TS2688: Cannot find type definition file for '.github'.
error TS2688: Cannot find type definition file for 'build'.
error TS2688: Cannot find type definition file for 'examples'.
error TS2688: Cannot find type definition file for 'scripts'.
error TS2688: Cannot find type definition file for 'src'.
error TS2688: Cannot find type definition file for 'website'.

undefined
>

Apologies, I have searched for this, but wasn’t able to find anything relevant or within the last few months.

What is happening and why am I getting these weird errors? Is there any way they can be improved if it is in fact something that I’ve done wrong?

About this issue

  • Original URL
  • State: closed
  • Created 6 years ago
  • Reactions: 47
  • Comments: 56 (5 by maintainers)

Commits related to this issue

Most upvoted comments

These errors occur when you have subdirectories of a typeRoots directory (in this case node_modules/@types) that do not contain index.d.ts files. I agree the error message is mysterious and should be improved.

In your case, the errors occur because your package.json specifies a package named @types/, which is a silly thing to do.

I have this error today! But the code still got compiled to js 😶 error image

I met the same problem (‘cannot find the definition file for “babel__core”’) as you guys, but I googled it and I found the solution which works for me. 编译typescript出现 Cannot find type definition file for ‘babel__core‘

For those who cannot read Chinese, the solution is :

  1. option 1: add "skipLibCheck": true, in "compilerOptions" in your tsconfig.json
{
  "compilerOptions": {
     "skipLibCheck": true,                     /* Skip type checking of declaration files. */
  }
}
  1. option 2: add typeRoots in "compilerOptions" in your tsconfig.json
{
  "compilerOptions": {
    "typeRoots": [
      "node_modules/@types",
      "src/typings"
    ]
  }
}

In my case, originally I had "skipLibCheck": true, when I met this problem, so this time, I add "typeRoots" which solved my problem.

This is what I used that appears to remedy this type of error for me. If types is specified, only packages listed will be included. For instance: { “compilerOptions”: { “types” : [“node”, “lodash”, “express”] } } This tsconfig.json file will only include ./node_modules/@types/node, ./node_modules/@types/lodash and ./node_modules/@types/express. Other packages under node_modules/@types/* will not be included. A types package is a folder with a file called index.d.ts or a folder with a package.json that has a types field.

I got this problem too and my case is different. My project has the following file structure:

├ frontend
│  ├ node_modules/
│  ├ tsconfig.json
│  └ index.ts
└ backend

The frontend working directory is frontend, it has the node_modules directory inside and all the commands are run from this directory. I accidentally ran npm install something while being in the root directory so an excess node_modules directory appeared:

├ frontend
│  ├ node_modules/
│  ├ tsconfig.json
│  └ index.ts
├ node_modules/
└ backend

And then when I ran cd frontend && tsc --noEmit I got the TS2688 error.

I fixed the error by deleting the node_modules directory from the project root.

Sorry for do not having time read through all comments here. I think this error just indicated you:

"if you config tsc to do the job in this way, you need to install the missing type definitions for the modules that tsc indicate. For Example, in my scenario, tsc told me I’m missing type definition for “node”, then I solve it by yarn add -D @types/node`. 🧀

I have fixed this by adding "baseUrl": "." in my tsconfig.json file

Proposed new errors:

  • When types is not specified (this seems to be the case that mystifies the most users): Subdirectory '{0}' of 'typeRoots' directory '{1}' is not a valid types package. If the presence of this subdirectory is intentional, change the 'typeRoots' or 'types' option. See https://www.typescriptlang.org/docs/handbook/tsconfig-json.html#types-typeroots-and-types .
  • When types is specified: Could not find a types package named '{0}' (specified in the 'types' option) in any directory specified by the 'typeRoots' option. See https://www.typescriptlang.org/docs/handbook/tsconfig-json.html#types-typeroots-and-types .

(Of course, the long link could be replaced with an aka.ms link.)

I met the same problem (‘cannot find the definition file for “babel__core”’) as you guys, but I googled it and I found the solution which works for me. 编译typescript出现 Cannot find type definition file for ‘babel__core‘

For those who cannot read Chinese, the solution is :

  1. option 1: add "skipLibCheck": true, in "compilerOptions" in your tsconfig.json
{
  "compilerOptions": {
     "skipLibCheck": true,                     /* Skip type checking of declaration files. */
  }
}
  1. option 2: add typeRoots in "compilerOptions" in your tsconfig.json
{
  "compilerOptions": {
    "typeRoots": [
      "node_modules/@types",
      "src/typings"
    ]
  }
}

In my case, originally I had "skipLibCheck": true, when I met this problem, so this time, I add "typeRoots" which solved my problem.

it worked for me thank you!, 2nd option was the one who worked for me.

This should probably be a warning rather than an error. A missing typedef is equivalent to an empty typedef, which isn’t an error condition.

Aha! Doh!

My apologies, clearly that’s a yarn add gone wrong. Yes, very silly indeed.

Feel free to use my blunder-ticket to track improving any feedback 😉

I had a similar problem in a project where a library added native types, fixed by removing its @types/ lib.

In my case the library was yup, so removing @types/yup fixed the error.

Typescript authors: the error message is not helpful. It would be nice to detect overlapping types and give an error about a conflict, perhaps like “Try removing the @types library if one is installed”. 👍

The entry "@types/": "reach/router" caused https://github.com/reach/router to be downloaded directly into the node_modules/@types folder, creating new files and subdirectories unrecognized by the TypeScript compiler alongside the existing valid subdirectories. I’m guessing you ran yarn add @types/@reach/router, trying to install the @types package for the scoped package @reach/router, but that command is actually parsed as installing a package named @types/ at version reach/router. You probably meant @types/reach__router: that’s the naming convention for @types packages for scoped packages. Consider filing a bug against Yarn for letting you install a package with the invalid name @types/. (I notice that NPM correctly catches this.)

solution

**Solution of above error ** Run this : npm install @types/node --save-dev

and in tsconfig file add :
{ “compilerOptions”: { “types”: [“node”] } }

i have the same error but on jsconfig.json file, for no aparent reason, i don’t use babel or any other transpiler on my project, because its a very simple static website, and this is what i have on my jsconfig file, and it’s reporting an “unexpected” error, i don’t use babel, or any other transpiler, how can i get rid of this error? or is this a bug?

image

This is still a problem, I get Cannot find type definition file for 'node_modules'.

I have

  1. "typeRoots": [ "./node_modules/@types",
  2. "baseUrl": ".",
  3. "moduleResolution": "node",
  4. and my node_modules is just located at project root, no additional node_modules added.

So basically I tried all methods above, nothing works. And this error message is still a cipher in 2021.

This worked for me https://stackoverflow.com/a/72759322/13987596

That’s the only way that works for me.

I tried all other ways above, but none of them worked.

Big thanks!

I met the same problem (‘cannot find the definition file for “babel__core”’) as you guys, but I googled it and I found the solution which works for me. 编译typescript出现 Cannot find type definition file for ‘babel__core‘

For those who cannot read Chinese, the solution is :

  1. option 1: add "skipLibCheck": true, in "compilerOptions" in your tsconfig.json
{
  "compilerOptions": {
     "skipLibCheck": true,                     /* Skip type checking of declaration files. */
  }
}
  1. option 2: add typeRoots in "compilerOptions" in your tsconfig.json
{
  "compilerOptions": {
    "typeRoots": [
      "node_modules/@types",
      "src/typings"
    ]
  }
}

In my case, originally I had "skipLibCheck": true, when I met this problem, so this time, I add "typeRoots" which solved my problem.

This worked for me. Thank you!

JUST FYI for Everyone:

I found this thread reading having this same issues. But in mine i had removed the library and @type file as no longer needed. I was still getting this error.

Restarting VSCODE and it went away

Exact same thing happened to me as @mattmccutchen describes. Also ran yarn add @types/@scoped/package, and suddenly you have @types/ as dependency and these weird errors. Would be nice if we get a more descriptive error.

@houh60 damn. it worked for me too LOL! Sometimes modern problems required not-so-modern solutions.

These errors occur when you have subdirectories of a typeRoots directory (in this case node_modules/@types) that do not contain index.d.ts files. I agree the error message is mysterious and should be improved.

In your case, the errors occur because your package.json specifies a package named @types/, which is a silly thing to do.

So… what’s the best strategy to tackle the need for index.d.ts? I currently keep an empty index.d.ts, with just a link to this issue as a comment. Next to it, I keep a bunch of smaller d.ts files.

I am not really happy with the empty index file strategy, but it seems to help - otherwise I simply can’t have a bunch of smaller d.ts files in my project’s types/ folder and TS2688 bites me…

O mesmo erro aconteceu comigo, nisso ao ler esse fórum inclui a opção 2 do nosso amigo acima e deu super certo, obrigada!

Opção 2: adicionar typeRoots em “compilerOptions” em seu tsconfig.json

{ “compilerOptions”: { “typeRoots”: [ “node_modules/@types”, “src/typings” ] } }

For me None of the above solutions worked! the case occured when I was installing xero-node My observations,

  • when xero-node is installed, build fails, before that, it works
  • when installing xero-node, inside node_modules/@types, a folder is added named keyv node_modules/@types/keyv
  • this folder doesnt have index.d.ts, which caused the build to fail, also its just a useless file saying, now on we dont need to install @types/keyv its read me says keyv provides its own type definitions, so you don't need @types/keyv installed!

Fix: Remove the keyv folder from node_modules/@types and try to build again! It worked for me!

I reached the same situation, where I need to add a root to look into for declaration file (typing a cordova plugin and need global access)

image

Same error, I have a tsconfig in a Cypress folder and one in root.

Ok. Thanks for your feedback. If you solved your problem, then why are you telling me? Why not just published it as a check that developers need to ascertain and forget it?

On Wed, Jan 8, 2020 at 5:18 AM Su notifications@github.com wrote:

Sorry for having time read through all comments here. I think this error just indicated you:

"if you config tsc to do the job in this way, you need to install the missing type definitions for the modules that tsc indicate. For Example, in my scenario, tsc told me I’m missing type definition for “node”, then I solve it by yarn add -D @types/node`. 🧀

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/microsoft/TypeScript/issues/27956?email_source=notifications&email_token=ANU7JYM5YEZ6BALZNRKDJVTQ4WY77A5CNFSM4F5Q5E6KYY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOEIMBYAA#issuecomment-572005376, or unsubscribe https://github.com/notifications/unsubscribe-auth/ANU7JYO4AGMPKVMCXJQPE2TQ4WY77ANCNFSM4F5Q5E6A .

Within the Typescript documentation with the section on compiler options ‘types’, it worked for me to create the types: [“anymatch”. “lodash”, …] compiler option in tsconfig.json to eliminate this error. I am using Visual Studio code. As you know this may or may not work for you. https://www.typescriptlang.org/docs/handbook/tsconfig-json.html

TypeScript looks in node_modules/@types by default since this is where types packages from DefinitelyTyped containing global declarations (such as @types/node, to give one popular example) are normally installed. See the documentation.