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?
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.
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" /> .
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.
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" /> .
create index.d.ts
file in folder src
,and add this line
declare module '*.jpg';
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
.
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
.
Create module/type
# src/types/images.d.ts
declare module '*.jpg';
declare module '*.jpeg';
Change tsconfig.json
{
"compilerOptions": {
"typeRoots" : ["node_modules/@types", "src/types"]
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With