Skip to content
Advertisement

How to import .js file inside my .tsx file

I am having an issue where webpack is telling me:

ERROR in ./app/app.tsx (4,25): error TS2307: Cannot find module ‘./sample-data’.

My imports look like this:

JavaScript

And lastly this is my sample-data.js file that I am trying to import:

JavaScript

If I change it to sample-data.ts I get told that it isn’t a module. How do I go about getting this loaded into my .tsx file?

Advertisement

Answer

If you want to import the original file without modifications try to set compilerOptions.allowJs to true in your tsconfig.json file:

JavaScript

In case you’re ready to modify the file you can turn sample-data.js into sample-data.ts by using the module syntax, like that:

JavaScript

You’ll then need to import the file in the following way:

JavaScript

You can read more on TypeScript modules in the official docs. In many cases modules in TypeScript work the same way as in ES2015.

Advertisement