Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

typescript: Cannot find module 'react'

I don't understand why it can't find it.

$ cat tsconfig.json  {   "compilerOptions": {     "sourceMap": true,     "target": "es6",     "jsx": "react",     "types": [       "lodash",       "react",       "react-dom"     ]   } }  $ tree node_modules/@types  node_modules/@types/ ├── lodash │   ├── README.md │   ├── index.d.ts │   ├── package.json │   └── types-metadata.json ├── react │   ├── README.md │   ├── index.d.ts │   ├── package.json │   └── types-metadata.json └── react-dom     ├── README.md     ├── index.d.ts     ├── package.json     ├── server.d.ts     └── types-metadata.json 

Importing in a component file.

// src/components/component.tsx  import * as React from "react"; 

What gives?

like image 521
AJcodez Avatar asked Nov 30 '16 23:11

AJcodez


People also ask

Can't find module in path react?

To solve the error "Cannot find module 'react/jsx-runtime' or its corresponding type declarations", make sure to install the typings for react running the command npm install --save-dev @types/react@latest @types/react-dom@latest and restart your dev server.

Could not find a declaration file for module react TypeScript?

The error "could not find declaration file for module 'react'" occurs when TypeScript cannot find the type declaration for a react-related module. To solve the error install the types for the module by running the command from the error message, e.g. npm install -D @types/react . Copied!

How do you solve Cannot find module or its corresponding type declarations?

The "Cannot find module or its corresponding type declarations" error occurs when TypeScript cannot locate a third-party or local module in our project. To solve the error, make sure to install the module and try setting moduleResolution to node in your tsconfig. json file.

Can not find module react Dom?

To solve the error "Module not found: Error: Can't resolve 'react-dom'", make sure to install the react-dom package by opening your terminal in your project's root directory and running the command npm install react-dom react and restart your development server.


2 Answers

I had the same issue. You just need to install @types:

npm i -D @types/react 
like image 144
Ihor Pavlyk Avatar answered Sep 21 '22 00:09

Ihor Pavlyk


if you're not using typescript 2.1, you should upgrade to it. it looks like you're using a 2.x version from the @types you have there.

here is a working tsconfig.json file that i'm using right now:

{   "compilerOptions": {     "target": "es6",     "module": "commonjs",     "moduleResolution": "node",     "noResolve": false,     "noImplicitAny": false,     "removeComments": true,     "sourceMap": true,     "allowJs": true,     "jsx": "react"   },   "exclude": [     "./node_modules/**/*"   ] } 

it's been a couple days since i resolved the same issue you were having, but i think the key here is the "moduleResolution": "node" and "allowJs": true.

like image 35
rkstar Avatar answered Sep 20 '22 00:09

rkstar