Last updated: Apr 7, 2024
Reading timeยท6 min
If you got the error 'Module not found: Can't resolve image', click on the second subheading.
To import and use an image in a React component:
import MyImage from './thumbnail.webp';
.src
prop on the img
element.<img src={MyImage} alt="horse" />
.// ๐๏ธ import the image import MyImage from './thumbnail.webp'; export default function App() { return ( <div> {/* ๐๏ธ local image */} <img src={MyImage} alt="horse" /> {/* ๐๏ธ external image */} <img src="https://bobbyhadz.com/images/blog/react-prevent-multiple-button-clicks/thumbnail.webp" alt="car" /> </div> ); }
We used an ES6 default import to import an image in a React application.
The alt
prop helps screen readers understand what the image is about.
Notice that the image that is self-closing <img />
.
The example assumes that you have an image named thumbnail.webp
in the same
folder as the App
component.
For example, if you are importing an image from one directory up, you would
import as import MyImage from '../thumbnail.webp'
.
The image has to be located in the src
directory of your project.
svg
image in your React applicationYou can use this approach to import and use png
, svg
, webp
, jpg
, etc,
images in your React app.
// ๐๏ธ import SVG image import MyImage from './logo.svg'; export default function App() { return ( <div> {/* ๐๏ธ local image */} <img src={MyImage} alt="logo" /> {/* ๐๏ธ external image */} <img src="https://bobbyhadz.com/images/blog/react-prevent-multiple-button-clicks/thumbnail.webp" alt="car" /> </div> ); }
public
directoryIf your images are located in the public
directory, use an absolute path
when setting the src
prop on the img
element.
For example, if you have an image located at public/images/thumbnail.webp
, you
would set the src
of the img
element to "/images/thumbnail.webp"
.
export default function App() { return ( <div> {/* ๐๏ธ local image */} <img src="/images/thumbnail.webp" alt="horse" /> {/* ๐๏ธ external image */} <img src="https://bobbyhadz.com/images/blog/react-prevent-multiple-button-clicks/thumbnail.webp" alt="car" /> </div> ); }
require()
functionIf you aren't able to use the ES6 import/export syntax in your setup, try using
require()
.
export default function App() { return ( <div> {/* ๐๏ธ local image */} <img src={require('./thumbnail.webp')} alt="horse" /> <img src={require('./logo.svg').default} alt="horse" /> </div> ); }
The example above uses the require()
syntax to import 2 images located in the
same directory as the App
component.
If you need to display an image from an external URL, set the src
prop on the
img
tag to the complete URL of the image.
export default function App() { return ( <div> <img src="https://bobbyhadz.com/images/blog/react-prevent-multiple-button-clicks/thumbnail.webp" alt="car" /> </div> ); }
The example shows how to display an image from an external URL.
We used the img
tag and set its src
prop to the complete URL that points to
the image.
The React.js error Module not found: Can't resolve image occurs when we specify an incorrect path to the imported image.
To solve the error, import the image and make sure the path that points to the image is correct in your import statement.
ERROR in ./src/App.js 4:0-43 Module not found: Error: Can't resolve './src/thumbnail.webp' in '/home/borislav/Desktop/bobbyhadz-rest/bobbyhadz-react/src'
Assuming there is a thumbnail.webp
image in the same directory as your
App.js
file, you would import it and render it as follows.
// ๐๏ธ import the image import MyImage from './thumbnail.webp'; export default function App() { return ( <div> {/* ๐๏ธ local image */} <img src={MyImage} alt="car" /> {/* ๐๏ธ external image */} <img src="https://bobbyhadz.com/images/blog/react-prevent-multiple-button-clicks/thumbnail.webp" alt="car" /> </div> ); }
The example assumes that you have an image named thumbnail.webp
in the same
folder as the App
component.
For example, if you are importing an image from one directory up, you would
import as import MyImage from '../thumbnail.webp'
.
If I move my App.js
component to a nested
directory and leave the image in
the src
directory, I would import the image from one directory up.
// ๐๏ธ import the image from one directory up import MyImage from '../thumbnail.webp'; export default function App() { return ( <div> {/* ๐๏ธ local image */} <img src={MyImage} alt="horse" /> {/* ๐๏ธ external image */} <img src="https://bobbyhadz.com/images/blog/react-prevent-multiple-button-clicks/thumbnail.webp" alt="car" /> </div> ); }
NOTE: the image has to be located in the src
directory of your project.
If the error persists when you use this approach, try to specify a relative path
to the image, e.g. import MyImage from './../thumbnail.webp';
.
// ๐๏ธ import the image from one directory up import MyImage from './../thumbnail.webp'; export default function App() { return ( <div> {/* ๐๏ธ local image */} <img src={MyImage} alt="horse" /> {/* ๐๏ธ external image */} <img src="https://bobbyhadz.com/images/blog/react-prevent-multiple-button-clicks/thumbnail.webp" alt="car" /> </div> ); }
You can use this approach to import and use png
, svg
, webp
, jpg
, etc,
images in your React app.
Here is an example that assumes there is a logo.svg
image right in the same
directory as my App.js
file.
// ๐๏ธ assumes logo.svg is in the same directory as App.js import MyImage from './logo.svg'; export default function App() { return ( <div> {/* ๐๏ธ local image */} <img src={MyImage} alt="horse" /> {/* ๐๏ธ external image */} <img src="https://bobbyhadz.com/images/blog/react-prevent-multiple-button-clicks/thumbnail.webp" alt="car" /> </div> ); }
public
directoryIf your images are located in the public
directory, use an absolute path
when setting the src
prop on the img
element.
For example, if you have an image located at public/images/thumbnail.webp
, you
would set the src
of the img
element to "/images/thumbnail.webp"
.
export default function App() { return ( <div> {/* ๐๏ธ local image */} <img src="/images/thumbnail.webp" alt="horse" /> {/* ๐๏ธ external image */} <img src="https://bobbyhadz.com/images/blog/react-prevent-multiple-button-clicks/thumbnail.webp" alt="car" /> </div> ); }
The example assumes that you have a thumbnail.webp
image located under
public/images/thumbnail.webp
.
require()
functionIf you aren't able to use the ES6 import/export syntax in your setup, try using
require()
.
export default function App() { return ( <div> {/* ๐๏ธ local image */} <img src={require('./thumbnail.webp')} alt="horse" /> <img src={require('./logo.svg').default} alt="horse" /> </div> ); }
The example above uses the require()
syntax to import 2 images located in the
same directory as the App
component.
I've also written a detailed guide on where you should store images in a React app.
If you need to display an image from an external URL, set the src
prop on the
img
tag to the complete URL of the image.
export default function App() { return ( <div> <img src="https://bobbyhadz.com/images/blog/react-prevent-multiple-button-clicks/thumbnail.webp" alt="car" /> </div> ); }
The example shows how to display an image from an external URL.
We used the img
tag and set its src
prop to the complete URL that points to
the image.
You can learn more about the related topics by checking out the following tutorials: