12

I am trying to import some local images into reactjs. But it's not working. I am using reactstrap to make a carousel.

This is the error:

Module not found: Can't resolve '../../assets/img-3.jpg' in 'C:\Users\adity\Desktop\foodcubo-dev\project\src\Layouts\header'

I tried to import the images using the import method, although the images are available in assets. Import is fine, I guess. The problem might be related to rendering. I don't know.

This is my code:

Header.js

import React, { Component } from 'react';
import imgone from '../../assets/img-1.jpg'
import imgtwo from '../../assets/img-2.jpg'
import imgthree from '../../assets/img-3.jpg'

import {
  Carousel,
  CarouselItem,
  CarouselIndicators,
  CarouselCaption
} from 'reactstrap';

const items = [
    {
    src: {imgone},
    altText: 'Slide 1',
    caption: 'Slide 1'
  },
    {
    src: {imgtwo},  
    altText: 'Slide 2',
    caption: 'Slide 2'
  },
    {
    src: {imgthree},
    altText: 'Slide 3',
    caption: 'Slide 3'
  }
];

class Header extends Component {
  constructor(props) {
    super(props);
    this.state = { activeIndex: 0 };
    this.goToIndex = this.goToIndex.bind(this);
    this.onExiting = this.onExiting.bind(this);
    this.onExited = this.onExited.bind(this);
  }

  onExiting() {
    this.animating = true;
  }

  onExited() {
    this.animating = false;
  }


  goToIndex(newIndex) {
    if (this.animating) return;
    this.setState({ activeIndex: newIndex });
  }

  render() {
    const { activeIndex } = this.state;

    const slides = items.map((item) => {
      return (
        <CarouselItem
          onExiting={this.onExiting}
          onExited={this.onExited}
          key={item.src}
        >
          <img src={item.src} alt={item.altText} />
          <CarouselCaption captionText={item.caption} captionHeader={item.caption} />
        </CarouselItem>
      );
    });

    return (
      <Carousel
        activeIndex={activeIndex}
        next={this.next}
        previous={this.previous}
      >
        <CarouselIndicators items={items} activeIndex={activeIndex} onClickHandler={this.goToIndex} />
        {slides}
      </Carousel>
    );
  }
}


export default Header;
SherylHohman
  • 16,580
  • 17
  • 88
  • 94
aditya kumar
  • 2,905
  • 10
  • 39
  • 79
  • 1
    what is the absolute path the the images? – lacostenycoder Feb 23 '19 at 17:30
  • /c/Users/adity/desktop/foodcubo-dev/project/src/assets – aditya kumar Feb 23 '19 at 17:32
  • 1
    @lacostenycoder this is the complete url – aditya kumar Feb 23 '19 at 17:32
  • @lacostenycoder it says about webpack but here i am using create-react-app – aditya kumar Feb 23 '19 at 17:40
  • maybe try https://facebook.github.io/create-react-app/docs/adding-images-fonts-and-files – lacostenycoder Feb 23 '19 at 17:44
  • Good for including coffee. Tip, read about 'minimum viable example' in the help pages, linked to at the top of every page. Basically, you should [edit](https://stackoverflow.com/q/54844152/5411817) to delete any code not absolutely necessary to reproduce the error. For example, a single image & import would work. This makes is much easier for volunteers to troubleshoot, (and smaller code is more welcoming.) Additionally, you should add the full path of the images to your post for reference (not just as a Comment). There scenarios where that info would be important. (eg. if it were above `src`) – SherylHohman Feb 23 '22 at 16:27
  • What is the path to this file? Please add the paths of the current js file, and of the image file into the body of your question. As the error relates to not being able to find the file, this info is necessary for volunteers to see. It helps focus our scope for debugging. – SherylHohman Feb 23 '22 at 16:40

7 Answers7

8

I did encounter the same problem and doing this fixed my problem:

./../../assets/img-3.jpg
Dharman
  • 30,962
  • 25
  • 85
  • 135
user3336160
  • 96
  • 1
  • 2
0

I had the same issue. For me the problem was that the folder I had created had a trailing blank space in it's name:

path in code: ./images/testimage.png actual path : ./images /testimage.png

as you can see, "path in code" != "actual path", hence, the path in code cannot be resolved.

The solution was to just remove any blank spaces from your folders names. I probably made this error while creating the folder.

James Riordan
  • 1,139
  • 1
  • 10
  • 25
kaanan
  • 1
  • I think it didn't answer the problem. Maybe you can try to be more accurate regarding this issue. – MatiasG Aug 24 '21 at 02:49
0

Solved!!!

Store image path as (testimage.png) instead of (./images/testimage.png), then concat ('./images/') to (testimage.png) when using.

0
<figure className = 'profile-image'>
    <img src = {require('../../assets/img/pic.jpeg')} alt = '' />
</figure>

Replace .jpg with .jpeg
I used <img src = {require('../../assets/img/pic.jpg')} alt = '' /> 

and it showed the same error also tried importing it and still did not work. All i did was to call the full name of my image file and not to abbreviate it.

0

I tried all but for me was solution is just rename file name to other name and use it with new name.

Adi
  • 1
  • 1
0

import admin_image from "../../assets/admin-image.png";

just provide a name for the content you want to get then provide the path if it is in src folder, if it is in public folder try the following import,

import admin_image from "/public/assets/admin-image.png"; (or) import admin_image from "/assets/admin-image.png";

SAM NIJIN
  • 1
  • 3
-2

or maybe you just did not spell the file, folder, or outlined a file path correctly...

  • 1
    As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Feb 17 '22 at 08:53
  • At best, this response could be a Comment. It does not meet any criteria for an Answer as defined by SO. Answers require exact code necessary to solve issues whenever possible. This response is pure conjecture, a random suggestion, not an exact answer. In general, if there is a typo, the proper response is to leave a Comment (with the corrected typo if possible), then vote to close the question, as it would not useful to future visitors. Please review topics in the help section linked to at the top of every page. Keep in mind this platform is designed to be used differently from forums. – SherylHohman Feb 23 '22 at 10:31
  • This does not provide an answer to the question. Once you have sufficient [reputation](https://stackoverflow.com/help/whats-reputation) you will be able to [comment on any post](https://stackoverflow.com/help/privileges/comment); instead, [provide answers that don't require clarification from the asker](https://meta.stackexchange.com/questions/214173/why-do-i-need-50-reputation-to-comment-what-can-i-do-instead). - [From Review](/review/late-answers/31088184) – SherylHohman Feb 23 '22 at 10:32
  • Duplicate. Accepted answer said just that, but also *gave the exact code necessary to solve to OP's issue*. TBF, that answer should have also been a comment b/c it's a "typo" bug. As a typo bug, the question itself is subject to closure by SO. However, notice their response otherwise qualified as an answer by providing an exact solution. That's one difference between comments & answers. As a duplicate & a non-answer your post is subject to deletion by SO. Hint: if you delete it yourself 1st, I think there is a badge to delete 1 post with negative reputation..check the badges link to verify. – SherylHohman Feb 23 '22 at 17:10