nuxt: require is not defined
Environment
latest versions
Reproduction
<img :src="require('~/assets/image.png')"> anywhere in vue component.
Describe the bug
require is not defined when using it such as…
<img :src="require('~/assets/image.png')">
Additional context
No response
Logs
at $id_9513b4f8 (file://./.nuxt/dist/server/server.mjs:10135:22)
at __instantiateModule__ (file://./.nuxt/dist/server/server.mjs:10596:9)
at __ssrLoadModule__ (file://./.nuxt/dist/server/server.mjs:10543:25)
at ssrImport (file://./.nuxt/dist/server/server.mjs:10568:13)
at $id_45a9f067 (file://./.nuxt/dist/server/server.mjs:9928:37)
at __instantiateModule__ (file://./.nuxt/dist/server/server.mjs:10596:9)
at __ssrLoadModule__ (file://./.nuxt/dist/server/server.mjs:10543:25)
at ssrImport (file://./.nuxt/dist/server/server.mjs:10568:13)
at $id_7689e89d (file://./.nuxt/dist/server/server.mjs:1935:37)
at async __instantiateModule__ (file://./.nuxt/dist/server/server.mjs:10596:3)
About this issue
- Original URL
- State: closed
- Created 3 years ago
- Reactions: 2
- Comments: 38 (5 by maintainers)
If you are using vite then
requirewill not work; it’s webpack-only. Instead, just use:if you’re using nuxt 3 just put the img src as
"assets/<whatever path to your img>".By default nuxt 3 will not use the
@for specifying the path in the Vue.jsSo dynamically loading images using urls should work like so:
UPDATE May 9th:
In production the above does not work, the images are still rendered as
assets/path/to/img.pngwhere a path with_nuxt/.../.../should be used to access the assets.To dynamically render images that are currently existing within your assets directory, you will need to import them in your
scripttag.For example,
Again, this is for Nuxt 3
@pixelpaulaus You can set
vite: falsein yournuxt.config.Yeah found it https://vitejs.dev/guide/assets.html#new-url-url-import-meta-url - Vite docs
Thanks @juancarlos-ctg. This solution works with Nuxt3. But how to import the image dynamically ? My component has a String prop with the asset name and I would like to import the image depending on this prop.
For dynamic images use this, it works in nuxt 3. Define the path as I did.
Note: define your directory properly, my image are saved in images folder.
It work for me 😃
Even with
import * as MY_VAR from 'package'?We now know that require does not work with vite, but which could be the solution to import the image dynamically with ssr?
For example, I was importing the image using “require” and combining it with a string prop, but can’t see a way of doing something similar with vite ssr, as import.meta.url is not working.
An example of changing images dynamically while using Vite:
<img :src="`/assets/${dynamic_image_name}.jpeg`"/>Update: It only worked on dev mode.
Alguna solución a este problema, para ssr ?
Check out https://vitejs.dev/guide/features.html#glob-import and follow https://github.com/nuxt/nuxt.js/issues/14766.
@slavco86 @manniL your discussion and solution is not related to this problem. The problem is/was to import/require a dynamic image path and not a package.
To the problem with loading dynamic images: I found a solution in stack-overflow: https://stackoverflow.com/a/71514878/7866722
My solution turned out like that
Although I am getting the following error now:
Full code for an Article Image with Nuxt Content
Bingo! Thanks!!!
I kind of found a work-around by replacing
<img>with a<div>and setting the background-image with a Tailwind class. Changed from:to:
This works in production too, but only when passing the whole Tailwind class as a single string.
Concatenating the class name only works with
yarn dev:yarn buildexits with an error:It’s no one-to-one replacement, but at least the images are rendered in production and are available at e.g.
http://www.example.io/_nuxt/logo-1.ccb82e2f.svg.Might be helpful to someone
https://github.com/nuxt/framework/discussions/7391
Can you not
importinstead ofrequire? (I would request that you move this to a discussion rather than a long-closed and unrelated issue.)not work in ssr