TypeScript icon indicating copy to clipboard operation
TypeScript copied to clipboard

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

Open atrauzzi opened this issue 7 years ago β€’ 44 comments

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?

atrauzzi avatar Oct 18 '18 00:10 atrauzzi

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.

mattmccutchen avatar Oct 18 '18 02:10 mattmccutchen

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

atrauzzi avatar Oct 18 '18 11:10 atrauzzi

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

mattmccutchen avatar Oct 18 '18 15:10 mattmccutchen

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

atrauzzi avatar Oct 18 '18 16:10 atrauzzi

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.

mattmccutchen avatar Oct 18 '18 16:10 mattmccutchen

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

atrauzzi avatar Oct 18 '18 17:10 atrauzzi

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

mattmccutchen avatar Oct 18 '18 19:10 mattmccutchen

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.

atrauzzi avatar Oct 18 '18 19:10 atrauzzi

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.

Finesse avatar Jul 08 '19 10:07 Finesse

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.

jasperkuperus avatar Sep 21 '19 18:09 jasperkuperus

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.

Sesna avatar Sep 29 '19 04:09 Sesna

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

loopmode avatar Sep 30 '19 12:09 loopmode

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

Texas-Analyst avatar Dec 04 '19 21:12 Texas-Analyst

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.

Texas-Analyst avatar Dec 04 '19 21:12 Texas-Analyst

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:

Gfast2 avatar Jan 08 '20 11:01 Gfast2

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 [email protected] 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 .

Texas-Analyst avatar Mar 12 '20 01:03 Texas-Analyst

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.

thorsent avatar Jul 15 '20 19:07 thorsent

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

veritem avatar Sep 30 '20 08:09 veritem

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

neokamikai avatar Oct 10 '20 15:10 neokamikai

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)

iloseall avatar Oct 12 '20 07:10 iloseall

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

stoplion avatar Oct 21 '20 03:10 stoplion

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

blurk avatar Oct 24 '20 14:10 blurk

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 ?

thebiltheory avatar Dec 07 '20 10:12 thebiltheory

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

dagatsoin avatar Dec 08 '20 09:12 dagatsoin

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.

linonetwo avatar Feb 23 '21 08:02 linonetwo

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.

amelieykw avatar Mar 02 '21 01:03 amelieykw

@amelieykw Hi I solve this by an upgrade to the latest ts-node.

linonetwo avatar Mar 02 '21 08:03 linonetwo

@amelieykw Hi I solve this by an upgrade to the latest ts-node.

@linonetwo Congratulations! However, I don't think that the problems that we met were same. In my case, the problem was 'cannot find the definition file for "babel__core"' in app created by create-react-app.

amelieykw avatar Mar 02 '21 09:03 amelieykw

@amelieykw Setting only "skipLibCheck": true didn't help me. It worked when I added "typeRoots": ["node_modules/@types"] under "compilerOptions", though. What's interesting is that the error was reported for libraries I don't even reference explicitly in my package.json.

ivrtaric avatar Mar 31 '21 11:03 ivrtaric

I had the simillar issue and I was able to reslove by adding package.json "@types/systemjs": "~version" in devDependencies

yugakiran avatar Apr 09 '21 23:04 yugakiran