Twitter khuong291
Twitter khuong291

Reputation: 11672

./src/components/index.js Module not found: Can't resolve './{Component}' in '/src/components'

After I change all file from jsx to tsx, I get this error:

./src/components/index.js Module not found: Can't resolve './Header' in '/Users/khuongpham/WebFrontEnd/home/src/components'

enter image description here

Upvotes: 0

Views: 5227

Answers (2)

Harry
Harry

Reputation: 5707

If you changed all files from jsx to tsx then what is the reason for leaving index.js?
Why don't you change it to index.tsx or at least index.ts?

As far as I'm concerned, ts and js can't directly interact with each other.

If you are writting a ts or tsx file and you want to import a js file (or js library) you will need something called type declaration file which, most of the time, can be found here: https://microsoft.github.io/TypeSearch/

In your case, you are trying to import typescript from javascript and that is probably the problem. Changing index.js to index.ts may help.

Upvotes: 0

Carr
Carr

Reputation: 2771

You need to do correct Webpack configuration, add .tsx under resolve property:

....

resolve: {
    extensions: ['.js', '.jsx', '.tsx']
}
....

FYI: It seems if you want to integrate TypeScript into React, you still need to install some libraries and development dependencies to let Webpack and React play well.

Upvotes: 1

Related Questions