Company logo
  • Jobs
  • Bootcamp
  • About Us
  • For professionals
    • Home
    • Jobs
    • Courses
    • Questions
    • Teachers
    • Bootcamp
  • For business
    • Home
    • Our process
    • Plans
    • Assessments
    • Payroll
    • Blog
    • Calculator

0

93
Views
JS - how to importe correctly image from public folder

I have an image in the public folder called logo.png

when I try to import it in my index.js file (src/view/index.js) :

import logo from "public/logo.png";

I always get this error :

Module not found: You attempted to import /public/logo.png which falls outside of the project src/ directory. Relative imports outside of src/ are not supported.
7 months ago · Juan Pablo Isaza
1 answers
Answer question

0

If you are using an <img> element to display the image, it will have a src attribute. In that case, you can use the image url for the soruce. It looks like this:

const logo = `${process.env.PUBLIC_URL}/logo.png`;

Then to render it, you would have something like this:

<img
    alt='my-logo'
    src={logo}
/>
7 months ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post job Plans Our process Sales
Legal
Terms and conditions Privacy policy
© 2023 PeakU Inc. All Rights Reserved.