1

Imports of png images fail to resolve locally (in development mode, running npm vite). They do resolve in the production build however. At first I imported them dynamically but they wouldn't resolve in the production build so I imported them beforehand.

//.ts file
import test from "../assets/sprites/test.png"
//vite.config.ts
export default defineConfig({
    plugins: [vue()],
    build: {
        target: 'esnext'
    }
})

test.png:1 GET http://localhost:3000/frontend/src/assets/frontend/src/assets/sprites/test.png 404 (Not Found)

1 Answer 1

5

To fix this issue, use new URL(url, import.meta.url) to resolve static assets both in prod and dev

See Vite documentation on Static Asset Handling : https://vitejs.dev/guide/assets.html#new-url-url-import-meta-url

const test = new URL('../assets/sprites/test.png', import.meta.url).href
Sign up to request clarification or add additional context in comments.

4 Comments

For me, this works in build but not for dev
Works in both here. What's your error ?
Not working for me. Looks like import ... from 'image.png' doesn't work in Vite.
😀😂Works for svg but not for png and not for jpg, vite 5.2.13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.