Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
atrauzzi opened this issue Oct 18, 2018 · 56 comments
Closed

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

atrauzzi opened this issue Oct 18, 2018 · 56 comments
Labels
Bug A bug in TypeScript Domain: Error Messages The issue relates to error messaging
Milestone

Comments

@atrauzzi
Copy link

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?

@mattmccutchen
Copy link
Contributor

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.

@atrauzzi
Copy link
Author

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 😉

@mattmccutchen
Copy link
Contributor

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

@atrauzzi
Copy link
Author

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

@mattmccutchen
Copy link
Contributor

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.

@weswigham weswigham added Bug A bug in TypeScript Domain: Error Messages The issue relates to error messaging labels Oct 18, 2018
@atrauzzi
Copy link
Author

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

@mattmccutchen
Copy link
Contributor

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

@atrauzzi
Copy link
Author

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.

@Finesse
Copy link

Finesse commented Jul 8, 2019

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.

@jasperkuperus
Copy link

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.

@Sesna
Copy link

Sesna commented Sep 29, 2019

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.

@loopmode
Copy link

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

@Texas-Analyst
Copy link

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
Copy link

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.

@Gfast2
Copy link

Gfast2 commented Jan 8, 2020

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

@Texas-Analyst
Copy link

Texas-Analyst commented Mar 12, 2020 via email

@thorsent
Copy link

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.

MattSwartz pushed a commit to InteropIO/finsemble-seed that referenced this issue Sep 3, 2020
* Allow core to be built on latest node version
- Use `>12` instead of `^12`

* Add quotes to copyfiles in package.json
- On MacOS the quotes are necessary
- Result is that ttf and img files don't get moved to /react dist folder
- import error on "./fonts/font-finance.ttf"

* Proper quoting of package.json commands
- Turns out that single quotes work on Mac but not Windows
- \" is the cross-platform way to quote
- Use \\\" to include a quote within a quoted argument! (none in here but FYI)
- Ultimately this is super-fragile. This build code should be moved downward into localized node code (gulp, node or cli)

* Updated yarn.lock from dependency changes

* Added comments to readme for future build strategy
- placeholder, project was deadended because of timelines

* Blank index.d.ts files to work around tsc idiosyncracy
- microsoft/TypeScript#27956
- Was throwing tsc errors that caused build to break in production mode

* Refactor of core build process
- Simplified package.json
- DRY webpack configs
- Cleaned up readme

* Dummy index.d.ts files in package.json
- For tsc idiosyncracy

* Added standalone "launch" to seed
- Launches finsemble without build or server

* Removed duplicate function call
- Caused webpack to break in production mode

* Added error output to seed webpack
- Needed to read "stats" object for errors
- Commented out warnings because we have too many to resolve right now

* Added top level prod build to monorepo
- `yarn prod` to run in production
- Non-parallelized build. Total refactor needed to do otherwise.

* Eliminated packaged finsemble.css in favor of files
- Bug in mini-css-export-plugin was maddening
- Decided to caveman the problem and just copy files from node_modules into assets
- In the end, this is more web and less webpack, so better and faster
- Work being done in CopyWebpackPlugin but ought to be refactored because it's not really a webpack task at all.

* Remove "finsemble" and "installer-tmp" on clean
- installer-tmp can get left hanging around
- finsemble will get copied again with next build

* Caching for production mode is now zero
- This was tripping us up over and over in development
- If you ran production mode just once, then no code changes would take effect for 24 hours
- Instead, we added some guidance on setting up production mode as comments within the file
@veritem
Copy link

veritem commented Sep 30, 2020

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

@neokamikai
Copy link

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

@iloseall
Copy link

iloseall commented Oct 12, 2020

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)

@mosesoak
Copy link

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

@JamesBarrettDev
Copy link

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

Thanks! Had the same problem with @types/yup this worked. 😃

@timmeade
Copy link

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

@AliRadmanesh
Copy link

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

As @mosesoak said, it's because you have types for packages that you don't even use in @types folder.

So first of all try to remove those packages or try removing node_modules and yarn.lock and reinstall your packages.

If that doesn't help, go for other options like typeRoots in tsconfig.json.

@arvndvv
Copy link

arvndvv commented Oct 20, 2022

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!

@JRasmusBm
Copy link

This is what worked for me: #27956 (comment)

The rogue node_modules folder was in the great-grandparent directory. Took me just about 2 hours to figure out 😢 🤣

@thorsent
Copy link

thorsent commented Nov 3, 2022

Missing index.d.ts unnecessarily broke a chunk of the internet:
jaredwray/keyv#528

"This should be a warning", he says again 2 years later.

@AijazAsif
Copy link

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.

Thank you

@TechStudent10
Copy 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.

This worked for me. Thank you!

@dayknlang
Copy link

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"
]
}
}

@cangSDARM
Copy link

cangSDARM commented Feb 15, 2023

I don't know why this error comes up, can't there be no need for node_modules to be defined type by default? Does it have to have @types??why

@Sazzels
Copy link

Sazzels commented Feb 24, 2023

Initially I ran into the same problem. My tsconfig.json always showed me that Cannot find type definition file for 'node'.

grafik

However, if I thoroughly follow the instructions for "Nuxt 3 getting-started" which is where this error appeard for me, the error clears up. There it recommends to use a Plugin Volar Extension https://marketplace.visualstudio.com/items?itemName=Vue.volar.

I think the important part is ...enable Take Over Mode (recommended).

Found in here in https://nuxt.com/docs/getting-started/installation#prerequisites which leads to https://vuejs.org/guide/typescript/overview.html#volar-takeover-mode

After enabling Take Over Mode which requires also to restart the current worspace the error is resolved.

grafik

Maybe there are similar conflicts with Plugins for your Frameworks or disabling TypeScript and JavaScript Language Features is a bad workaround which disables the checking entirely (if this is case: sorry 😅)

@generalMashuka
Copy link

rm -rf node_modules
rm package-lock.json
npm i

fixed my strapi error TS2688

@MalekSmida
Copy link

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

@lufercampos
Copy link

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

a VSCODE restarting fixed the errors...thank you.

@houh60
Copy link

houh60 commented Jun 15, 2023

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!

@amandesai01
Copy link

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

@pocin
Copy link

pocin commented Sep 9, 2023

@RyanCavanaugh
Copy link
Member

This is turning into a random support thread which is not the intention of the issue tracker. I'm not seeing a putatitive defect here - please seek support in other fora if needed.

@microsoft microsoft locked as resolved and limited conversation to collaborators Sep 15, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Bug A bug in TypeScript Domain: Error Messages The issue relates to error messaging
Projects
None yet
Development

No branches or pull requests