search

Home  >  Q&A  >  body text

The URL of the static resource cannot be found in the Vite js development environment

Imported png images cannot be parsed locally (in development mode, run npm vite). However, they did fix it in the production version. At first, I imported them dynamically, but they didn't resolve in the production version, so I imported them ahead of time.

1

2

//.ts file

import test from "../assets/sprites/test.png"

1

2

3

4

5

6

7

//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)

P粉895187266P粉895187266551 days ago918

reply all(1)I'll reply

  • P粉494151941

    P粉4941519412023-10-26 00:22:17

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

    See the Vite documentation on static asset handling: https://vitejs.dev/guide/assets.html#new-url-url-import-meta-url

    1

    const test = new URL('../assets/sprites/test.png', import.meta.url).href

    reply
    0
  • Cancelreply