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

Created on 18 Oct 2018  ยท  24Comments  ยท  Source: microsoft/TypeScript

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?

Bug Error Messages

Most helpful comment

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.

All 24 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.

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 ๐Ÿ˜‰

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

In my situation, how was the directory @types being inferred? Does TS read package.json for hints?

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.

So how does that connect back to there being a bad @types/ entry in my package.json?

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

Aha! Gotcha. After reading your error messages, I wasn't so sure they'd have helped me figure out what was going on either. So, I was trying to think if there's any way to highlight not necessarily the source of my error, but better information about the symptom.

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.

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.

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.

it help me a lot, thank you.

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

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

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.

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`. :cheese:

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
.

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.

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

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

image

tsc --project ./
I got error output

error TS2688: Cannot find type definition file for 'cacheable-request'.
error TS2688: Cannot find type definition file for 'eslint'.      
error TS2688: Cannot find type definition file for 'eslint-scope'.
error TS2688: Cannot find type definition file for 'estree'.
error TS2688: Cannot find type definition file for 'http-cache-semantics'.
error TS2688: Cannot find type definition file for 'keyv'.
error TS2688: Cannot find type definition file for 'normalize-package-data'.
error TS2688: Cannot find type definition file for 'responselike'.

vscode 1.5.0
typescript 4.0.3 (npm install -g typescript)

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

I have this error today! But the code still got compiled to js ๐Ÿ˜ถ error image

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.

Does that mean you have to add each @types/<package> manually to your tsconfig ?

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

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Roam-Cooper picture Roam-Cooper  ยท  3Comments

seanzer picture seanzer  ยท  3Comments

DanielRosenwasser picture DanielRosenwasser  ยท  3Comments

uber5001 picture uber5001  ยท  3Comments

wmaurer picture wmaurer  ยท  3Comments