Skip to content

How might I obtain a URL path to images in my project? #26

Answered by ElMassimo
stanleypliu asked this question in Q&A
Discussion options

You must be logged in to vote

Hi! Happy to hear that it's working well for you.

Using @fs paths will not work in production, so I'd recommend to avoid using them directly.


If you only need a path to the default image, you could import it:

import ecommerceImagePath from '~/images/photos/ecommerce.jpg'

In development it will be the same path you wrote by hand, but it also works in production, and is less error prone since moving or deleting the image will cause the build to fail.


If you need to do this for many images and don't want to import them manually, you could use import.meta.globEager:

const photos = import.meta.globEager('../images/photos/**/*')

const imagePath = photos['../images/photos/ecommerce.jpg'].default

Replies: 1 comment 5 replies

Comment options

You must be logged in to vote
5 replies
@ElMassimo
Comment options

@stanleypliu
Comment options

@ElMassimo
Comment options

@stanleypliu
Comment options

@ruanltbg
Comment options

Answer selected by ElMassimo
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
3 participants