reactjspathname

i want to locate the file in visual studio code that is inside public folder


i want to locate the pathname for the image that i want to use in the project but i can't find the image. I have tried giving like this '../images/logo.png', but it shows Module not found: Can't resolve './images/logo.png' in 'C:\Users\55590i5 D'\Desktop\react-website-v2-master\src\components'

the file i.e. logo.png to access is inside public->images->logo.png and i want to import it inside src->components->navbar.js.


Solution

  • You aren't going quite far up enough in the directory tree with that path. Starting from navbar.js, these would be the parts of the path:

    1. ../ (up to /components)
    2. ../ (up to src)
    3. ../ (up to the project root)
    4. public
    5. images
    6. logo.png

    so the full relative path would be ../../../public/images/logo.png.

    However, if that's a create-react-app project, you can't import from public when you are inside src. Usually you would put an image you're trying to import somewhere under src (like in a src/assets folder, for example). See https://create-react-app.dev/docs/adding-images-fonts-and-files for more on this.