I'm working on a React application and using Webpack & Typescript. I would like to use an image in one of the <img/>
tags. However, I did not find the proper way to have access to the image files.
webpack.config.js:
... module: { rules: [ ... { test: /\.(png|jpe?g|svg)$/, loader: 'file-loader', options: { name: 'assets/[name].[ext]', } } ]
app.tsx:
... render() { return <img src='/assets/logo-large.png' alt="logo"/> }
When running the app, the assets/logo-large.png
resource is not found.
This is why webpack exists. It's a tool that lets you bundle your JavaScript applications (supporting both ESM and CommonJS), and it can be extended to support many different assets such as images, fonts and stylesheets.
Webpack is a popular module bundling system built on top of Node. js. It can handle not only combination and minification of JavaScript and CSS files, but also other assets such as image files (spriting) through the use of plugins.
Loved by many, hated by some, known to all. And still the most popular bundler in 2021. With more than 15 million weekly downloads (at the time of writing this post), there's no doubt that Webpack is still the bundler par excellence in 2021.
According to the official site, Webpack is a static module bundler for modern JavaScript applications. It builds a dependency graph that is responsible for bundling all your javascript modules into one regardless where they are located when one javascript file depends on each other.
Alternatively, in your custom_typings folder (if you have that), you can add a new import-png.d.ts
file:
declare module "*.png" { const value: any; export default value; }
So you can import an image using:
import myImg from 'img/myImg.png';
Alternatively, as reported by @mario-petrovic, you sometimes need to use a different export option as below (export = syntax). See here for the differences between the two approaches:
declare module "*.png" { const value: any; export = value; }
In which case you probably need to import the image as:
import * as myImg from 'img/myImg.png';
after spending some time to figure out the solution, this is what I did...
ensure that you have installed file-loader
as a dev dependency by
npm install -D file-loader
, if you use yarn yarn add -D file-loader
add the loader corresponding to the file extension in the Webpack rules webpack.config.js
, like this
module: { rules: [ ..., { test: /\.(png|jpe?g|gif|jp2|webp)$/, loader: 'file-loader', options: { name: '[name].[ext]', }, }, ], },
create an index.d.ts
file next to your tsconfig.json
file, actually you can name it whatever you want but you have to follow step 4.
Since Webpack now is going to handle several image extensions so you can add other image formats supported by file-loader
declare module '*.png'; declare module '*.jpg';
go to your tsconfig.json
file and add index.d.ts
to the include array like this:
{ "compilerOptions": { ..., "jsx": "react", "esModuleInterop": true, "target": "ES2020", "moduleResolution": "node" }, "exclude": ["node_modules", "**/*.spec.ts", "**/*.test.ts"], "include": ["src", "index.d.ts"] /// <-- Like this!! }
be aware that if you haven't defined an include
array, typescript by default is going to add all the files on the root folder, if you just define one file and not the file that contains all your code it is not going to compile. I think is a good practice to have all your code within the src folder.
Voila!!
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