2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

vue3 vite npm run build で 画像が見つからないエラー

Posted at

npm run dev ならエラーにならないが、
npm run build だとエラーになる。

原因は以下のコード

<img src="/img/seiya.jpg">

画像が見つかりませんよエラー

[vite]: Rollup failed to resolve import "/img/seiya.jpg" from "/var/www/html/renkatu.club/resources/js/components/Home.vue?vue&type=script&setup=true&lang.ts".
This is most likely unintended because it can break your application at runtime.
If you do want to externalize this module explicitly add it to
`build.rollupOptions.external`
error during build:
Error: [vite]: Rollup failed to resolve import "/img/seiya.jpg" from "/var/www/html/renkatu.club/resources/js/components/Home.vue?vue&type=script&setup=true&lang.ts".
This is most likely unintended because it can break your application at runtime.
If you do want to externalize this module explicitly add it to
`build.rollupOptions.external`
    at onRollupWarning (file:///var/www/html/renkatu.club/node_modules/vite/dist/node/chunks/dep-79892de8.js:46368:19)
    at onwarn (file:///var/www/html/renkatu.club/node_modules/vite/dist/node/chunks/dep-79892de8.js:46138:13)
    at Object.onwarn (file:///var/www/html/renkatu.club/node_modules/rollup/dist/es/shared/node-entry.js:25149:13)
    at ModuleLoader.handleInvalidResolvedId (file:///var/www/html/renkatu.club/node_modules/rollup/dist/es/shared/node-entry.js:23784:26)
    at file:///var/www/html/renkatu.club/node_modules/rollup/dist/es/shared/node-entry.js:23744:26

まじかよ

解決策

vite.configを以下のように書き換えるだけ

vue({
    // This is needed, or else Vite will try to find image paths (which it wont be able to find because this will be called on the web, not directly)
    // For example <img src="/images/logo.png"> will not work without the code below
    template: {
        transformAssetUrls: {
            includeAbsolute: false,
        },
    },
}),

2
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?