161

I created a new project using create-react-app and yarn 2 in vs code. The editor throws errors while importing every installed library like this:

Cannot find module 'react' or its corresponding type declarations.

The project compiles and runs successfully but the errors are still there. When I change the file's extension to .js and .jsx from.ts and .tsx, the errors disappear. How should I solve this problem for typescript files?

0

17 Answers 17

217

Maybe just restarting the TS server can work.

Using VsCode:

Windows:

type: ctrl + Shift + P or ctrl + Shift + F5

choose: > TypeScript: Restart TS server

Macbook:

type: ⇧ + ⌘ + P or F1

choose: > TypeScript: Restart TS server

Maybe it's just a file error, check if you accidentally changed a file from .ts to .tsx or .tsx to .ts

Sign up to request clarification or add additional context in comments.

11 Comments

It works for me. But im searching for the solution that will help me to fix this issue without restarting ts server in vscode
@cyberfrogg Maybe was just some bug in vs code, this happens a lot. I think you don't need to worry too much about it.
This ts error mentioned in the question happened to me after removing and installing node modules. Restarting the ts server worked, thanks a lot.
I lol'd when this was the suggestion then did it and it worked :shrug:
Note, to restart the TS server in VsCode, you should have a .ts or .tsx file open before doing what this comment suggests. See: stackoverflow.com/questions/64454845/…
|
124

You need to install types for libraries you install. generally you should do:

npm install @types/libName // e.g. npm install @types/react-leaflet

Some times there's no types available in DefinitelyTyped repo and you encounter npm ERR! 404 Not Found: @types/my-untyped-module@latest. You can do several things in this occasion:

1- Create a decs.d.ts file in root of your project and write in it:

    declare module "libName" // e.g declare module 'react-leaflet'

2- Or simply suppress the error using @ts-ignore:

// @ts-ignore  
import Map from 'react-leaflet'

3- Or you can do yourself and others a favor and add the library types in DefinitelyTyped repo.

If it still doesn't work, I firstly recommend that you use a recent version of Typescript. if you do, then close your editor, delete node_modules folder and install the dependencies again (npm install or yarn install) and check it again.

7 Comments

It's already installed for react, but the problem is still established.
@AmirFakhimBabaei you must do it for every installed library.
"Create a decs.d.ts file in root of your project and write in it" ... which project? The project for my dependent module or the project that includes it???
@StartupGuy Your project which includes the module without ts types.
wow. it's 2021 and we're doing *.d.ts files. React gives you a sparkplug and tells you that you can build a car. yeah. ok. it's a super framework.
|
48

I had the same error message, also related to the imports.

In my case, the problem was caused by importing a png file in order to use it in an SVG:

import logo from 'url:../../public/img/logo.png';

The message I received was:

Cannot find module 'url:../..public/img/logo.png' or its corresponding type declarations.

Solution:

In project root is a file css.d.ts. One must declare a module for the various graphics extensions (types) that are imported. (Note: Not for graphics types that are used or referenced in the project, but for those that are imported )

Here is the complete css.d.ts file for this project:

declare module '*.scss' {
  const css: { [key: string]: string };
  export default css;
}
declare module '*.sass' {
  const css: { [key: string]: string };
  export default css;
}
declare module 'react-markup';
declare module '*.webp';
declare module '*.png';
declare module '*.jpg';
declare module '*.jpeg';

3 Comments

maybe this comment can help guide people here working in Sapper (Svelte) projects: using Typescript in Sapper, a file src/ambient.d.ts will be generated that will already declare types for a couple of extensions. That is the perfect place to add yours.
@SillyFreak Thanks, great feedback. Please feel free to take this answer and create from it your own answer exactly as you suggest. Go right ahead and create a new answer in the Sapper / Svelte tag categories and borrow from this answer as necessary to help your fellow Sappers. This way, you will know the exact wording of the error messages or other key words that would bring googlers to your answer. Meilleurs voeux!
As noted by @Najathi, if css.d.ts (or whatever you call it) isn't included in the tsconfig.json "include": [...] array the ts server won't pick it up.
22

my situation:

myFolder is under the src dir

I see the same error in this line:

import { GlobalStyle } from "myFolder";

other answers dont work.

I simply add baseUrl to tsconfig.json after compilerOptions:

{
  "compilerOptions": {
    "baseUrl": "src/",
//other config...
  },
  "include": [
    "src"
  ]
}

issue solved!

ref: https://www.typescriptlang.org/tsconfig#baseUrl

Comments

8

I have deleted one module and added back it to my source code. Although the same source code is there, due to caching I am getting this error.

Below Solution works for me:

Windows:

ctrl + Shift + F5

Macbook:

⇧ + ⌘ + P OR F1

enter image description here

3 Comments

👀 Looks like my answer haha, but yeah this worked for me too bro +1 😄
@Erick Willian (⇧ + ⌘ + P OR F1 ) <- Write this line to your solution, after that might be our solution look the same.
Any idea why I don't see this option? Do I have to install an extension for that? I would assume VSCode offers this by default?
6

I fixed my issue

  1. create file /types/css.d.ts
  2. open tsconfig.json

add this object:

"include": [
    "src",
    "types"
]

Comments

2

In VSCode, choose: > TypeScript: Reload Project

This work for me.

Comments

1

In my case I had my file named index.tsx whereas it should have been with capital I, so Index.tsx.

Comments

1

This can still happen with newest versions of yarn + PnP, due to a known bug. Source

  1. Remove previous SDKs typescript folder, if present on .yarn/sdks/typescript
  2. Install previous supported TS version: yarn add [email protected]
  3. Add the new editor SDKs: yarn dlx @yarnpkg/sdks vscode. You can append vim o whichever supported SDKs.
  4. Restart your IDE

2 Comments

Thank you so much! That worked for me. I had been searching for hours and could not find anything that would solve the issue for me.
It works for typescript at version 5.0 if you don't want to get too outdated
0

Check the dependencies object in package.json file. If the install package is in the format "@somepackage/packagename":version; then at the time of import you must use import abc from "@somepackage/packagename"

If "@somepackage/" is not there then don't use it at the time of import. Just simply use import abc from "packagename";

Comments

0

In my case it was just an autocomplete import (like "@reduxjs/toolkit/src/query/tests/mocks/server") done by the VS Code that I didn't notice while commiting

Comments

0

I solved this problem for myself using the following import ..."/index.cjs": import { MRT_Localization_RU } from "mantine-react-table/locales/en/index.cjs";

Comments

0

In my case I solved it with restarted Typscript compiler:

npm install ts-node --save-dev
npm install typescript -g
npm install typescript --save-dev

Comments

0

Open tsconfig.json file and uncomment the below 2 lines:

"module": "nodenext" /* Specify what module code is generated. */,

"moduleResolution": "nodenext" /* Specify how TypeScript looks up a file from a given module specifier. */,

Here I have added my node Version. Then restart the Editor to reflect the changes. It works fine for me.

1 Comment

Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.
0

Using .js extension at the end worked for me even though it is database.types.ts file

import { Database } from "./database.types.js";

Comments

0

In my case removing the package-lock.json and installing packages with npm i fixed it.

Comments

0

I had this problem when I had the expected type in a file named alma.d.ts in a folder that also contained a regular alma.ts file. When I renamed the alma.ts file the error went away.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.