Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Importing images in TypeScript React - "Cannot find module"

I am trying to import images to use inside a React component with TypeScript. The bundler I'm using is Parcel (not Webpack).

I have created a .d.ts file inside the project with the image file extension, and included it inside tsconfig.json. However, when I try to import an image, TS yells at me about Cannot find module.

My project structure:

+ src
  + assets
    - image.jpg
  + components
    - Box.tsx
    - App.tsx
  - index.d.ts
  - index.html
  - index.tsx
- tsconfig.json
- tslint.json

I tried to import the image in App.tsx like this. VS Code underlined '../assets/image.jpg' and said Cannot find module '../assets/image.jpg'.

import * as React from 'react';
import * as img from '../assets/image.jpg';

const Box = props => {
  // do things...
}

export default Box;

The discussions I found online point to the need of defining a .d.ts file myself, so I created that index.d.ts file with this line.

declare module '*.jpg';

Then added "include": ["./src/index.d.ts"] inside tsconfig.json, after "compilerOptions" : {...}.

What did I miss? How can I fix the error TS is throwing?

like image 855
John Avatar asked Oct 11 '18 11:10

John


People also ask

How do I import images into TypeScript?

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.

How do I import an image into React?

To import and use an image in a React component:Import the local image, e.g. import MyImage from './thumbnail. webp'; . Pass the imported image to the src prop on the img element. For example, <img src={MyImage} alt="horse" /> .

Can not find module TS?

To fix the 'Cannot find module' error for paths that are in TypeScript tsconfig. json, we need to add all the paths that we want the TypeScript compiler to pick up. in tsconfig. json so that the src/modules/* and server/src/json/* directories and their children are all picked up by TypeScript compiler.

How do I display an image in React component?

To display an image from a local path in React:Download the image and move it into your src directory. Import the image into your file, e.g. import MyImage from './thumbnail. webp' . Set the src prop of the image element to the imported image, e.g. <img src={MyImage} alt="horse" /> .


3 Answers

create index.d.ts file in folder src,and add this line

declare module '*.jpg';
like image 72
王玉略 Avatar answered Oct 17 '22 09:10

王玉略


If you literally wrote "include": ["./src/index.d.ts"] in tsconfig.json and you don't have a "files" setting, that means the project defined by tsconfig.json includes only the single file ./src/index.d.ts. When you open any other file in VS Code, VS Code uses a separate language service instance that doesn't use your tsconfig.json. Adjust your "include" setting to match all the .ts and .tsx files in your project, or just delete it and rely on the default behavior of including all files under the directory containing tsconfig.json.

Round 2

TypeScript is ignoring index.d.ts because it assumes that index.d.ts is generated from index.tsx and index.tsx is more likely to be up to date. Name your index.d.ts file something else, e.g., declaration.d.ts.

like image 116
Matt McCutchen Avatar answered Oct 17 '22 09:10

Matt McCutchen


Create module/type

# src/types/images.d.ts
declare module '*.jpg';
declare module '*.jpeg';

Change tsconfig.json

{
    "compilerOptions": {
        "typeRoots" : ["node_modules/@types", "src/types"]
    }
}
like image 94
mrfsb Avatar answered Oct 17 '22 10:10

mrfsb