Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Typescript and React (with Gatsby loader): unable to import images as modules

First, I don't think Gatsby is involved in problem as runnning tsc -p . will detect the same compilation errors, before gatby's yarn start is ran.

I have a folder with some react tsx files I want to compile

src/
  @types/
    index.d.ts
  components/
    bottom/bottom.tsx
    layout.tsx
  images/
    email.png
    costs.png 
gatsby-config.js
gatsby-node.js
tsconfig.json
package.json

I want bottom.tsx to load email.png, but I have this error

TS2307: cannot load image module

As written in this post, I declare all pictures as modules in src/@types/index.d.ts. I have created a interface in this file for test urpose, and the file is correctly read by the compiler.

declare module '*.jpg';
declare module '*.png';​

export interface Thing {
    name: string
}

Using import as or adding content to module declaration won't change anything. However the code is running fine if I ignore typescript compilator :

//@ts-ignore
import email from '../../images/email.png'
//@ts-ignore
import logo from '../../images/logo-bw.png'

It works, so the structure of the code is ok with Gatsby, but obviously I lose a lot of benefits of using typescript as images is a big part of a website... Plus, there is no IDE autocompletion to help image import.

This Gatsby starter is made to be open source, so you can check the configuration at this branch : https://github.com/robusta-code/gatsby-the-robust/tree/0.0.1

Note that loading css or sass modules would be ok : import '../styles/home.scss'

like image 210
Nicolas Zozol Avatar asked Dec 11 '19 08:12

Nicolas Zozol


People also ask

How do you fix the Cannot find module error with importing images in TypeScript react?

To fix the "Cannot find module" error with importing images in TypeScript React, we should declare the . jpg module in a TypeScript type definition file. declare module '*.

How do I import an image into a TypeScript file?

To allow image import with TypeScript, we can use declare to declare the type for image files. to declare the type for modules with the . png extension.

Can you use TypeScript with Gatsby?

Since Gatsby natively supports JavaScript and TypeScript, you can change files from . js / . jsx to .

How to fix “cannot find module” error with importing images in typescript?

To fix the "Cannot find module" error with importing images in TypeScript React, we should declare the .jpg module in a TypeScript type definition file. ← How to check against custom type with ‘typeof’ in TypeScript? → How to use Node.js require inside a TypeScript file?

How do I integrate typescript with Gatsby?

TypeScript integration is supported through automatically including gatsby-plugin-typescript. Visit that link to see configuration options and limitations of this setup. If you are new to TypeScript, check out these other resources to learn more:

Can I use images in my react and typescript app?

In the last post, we added CSS to our Webpack configuration for our React and TypeScript app. In this post, we will extend this Webpack configuration so that images can be used in the app. If we run the app in dev mode ( npm start ), we see that the image isn’t found:

How to fix cannot find module error with importing images in react?

To fix the "Cannot find module" error with importing images in TypeScript React, we should declare the .jpg module in a TypeScript type definition file. in index.d.ts in the TypeScript project folder to let the TypeScript compiler know that .jpg files can imported into our project directly.


Video Answer


1 Answers

Wow... The problem is that my index.d.ts was exporting an interface !

In TypeScript, just as in ECMAScript 2015, any file containing a top-level import or export is considered a module. Conversely, a file without any top-level import or export declarations is treated as a script whose contents are available in the global scope (and therefore to modules as well)

Removing export interface Thing{} was enough.

like image 134
Nicolas Zozol Avatar answered Nov 15 '22 05:11

Nicolas Zozol