-
-
Notifications
You must be signed in to change notification settings - Fork 5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
require is not defined #12797
Comments
If you are using vite then <template>
<div class="app">
<img src="~/assets/image.png">
</div>
</template> |
okay, so i have found that it is using vite by default. Strange as i did not set that. So i assume nuxt3 uses Vite by default? I cant see anywhere in the docs to make it use webpack over vite. |
@pixelpaulaus You can set |
Yeah @pixelpaulaus, I had this same issue, how can I make a dynamic src using Vite? |
Yeah found it https://vitejs.dev/guide/assets.html#new-url-url-import-meta-url - Vite docs
|
not work in ssr |
Alguna solución a este problema, para ssr ? |
according to this page: https://vitejs.dev/guide/assets.html#new-url-url-import-meta-url::: Does not work with SSR This pattern does not work if you are using Vite for Server-Side Rendering, because import.meta.url have different semantics in browsers vs. Node.js. The server bundle also cannot determine the client host URL ahead of time. |
any solutions for this? |
if you're using nuxt 3 just put the img src as By default nuxt 3 will not use the So dynamically loading images using urls should work like so: <img :src="assets/path/to/img.png"> UPDATE May 9th: In production the above does not work, the images are still rendered as To dynamically render images that are currently existing within your assets directory, you will need to import them in your For example, <script>
import icon from '@/assets/images/icon.png'
export default {
data () {
return { image: icon}
}
}
</script>
<template>
<img :src="image">
</template>
</template Again, this is for Nuxt 3 |
Thanks @juancarlos-ctg. This solution works with Nuxt3. But how to import the image dynamically ? |
I'll try your solution. I finally put my images in the |
Any straightforward solution for this ? |
Yeah, I want to do something like this, but I can't:
|
Require does not work with vite which nuxt 3 uses |
We now know that require does not work with vite, For example, I was importing the image using "require" and combining it with a string prop, |
An example of changing images dynamically while using Vite:
Update: |
This works on SSR? |
Maybe use I have configuration like this, but nothing worked after building because of missing it I tried before many things, was googling for some hours reading documentations of rollup, nuxt, plugins, vite, commonjs etc. Then I went somewehere for 2 hours, I came back and repaired it within 5 minutes :D
|
Still not work |
I kind of found a work-around by replacing const logoFilename = 'logo-1.svg';
<img :src="`~/assets/img/logos/${logoFilename}`" ... /> to: const logoTailwindBgImage = 'bg-[url(~/assets/img/logos/logo-1.svg)]'
<div class="w-full ..." :class="logoTailwindBgImage" ...></div> This works in production too, but only when passing the whole Tailwind class as a single string. Concatenating the class name only works with const concatenatedTailwindClass = `bg-[url(~/assets/img/logos/${logoFilename})]`
<div class="w-full ..." :class="concatenatedTailwindClass" ...></div>
It's no one-to-one replacement, but at least the images are rendered in production and are available at e.g. Might be helpful to someone |
Trying to integrate Azure application insights with our Nuxt 3 app to monitor requests which Nuxt is making server side. Is there a work around? |
Can you not |
No, the package doesn't support |
Even with |
Bingo! |
@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: My solution turned out like that const imageUrl = new URL(`../../assets/images/${props.src}`, import.meta.url).href; Although I am getting the following error now:
Full code for an Article Image with Nuxt Content<!-- components/content/ArticleImage.vue -->
<template>
<img :src="imgSrc()" :alt="alt" class="article-image" />
</template>
<script setup lang="ts">
const props = defineProps<{
src: string;
alt: string;
}>();
function imgSrc() {
try {
const imageUrl = new URL(`../../assets/images/${props.src}`, import.meta.url).href;
return imageUrl;
} catch (error) {
console.error(error);
}
}
</script> |
Check out https://vitejs.dev/guide/features.html#glob-import and follow #14766. |
For dynamic images use this, it works in nuxt 3. <template>
<div class="app">
<img :src="`/_nuxt/assets/images/${img.src}`" alt="image"/>
</div>
</template>
export default {
data() {
return {
img: {
src: 'img.png'
}
}
}
} Note: define your directory properly, my image are saved in images folder. It work for me :) |
That one is more of a local-dev only thing. |
what if im trying to pass an object? like a json to a component's props (which is expecting an object)? |
I manged to get it working via a function in methods |
I have the same problem, what I'm trying to do is something like this: but it renders in the DOM as |
It's ridiculous that it's so complicated to get a dynamic image path in Nuxt. Surely, there's a better way? Thanks to #12797 (comment) This seems to work, but I don't like that the path is so fickle (relative) - it would be good if the alias worked: A working solution in Nuxt 3 using SFC ✅ <script lang="ts" setup>
const props = defineProps<{
backgroundImageName: string;
}>()
const getImageSrc = () => {
try {
const imageUrl = new URL(`../assets/images/${props.backgroundImageName}`, import.meta.url).href;
return imageUrl;
} catch (error) {
console.error(error);
}
}
</script>
<template>
<img :src="getImageSrc()" alt="background image" />
</template> |
Any news ? Have you guys find solution for that? |
Any solution? Doesn't work in Production with Netlify |
I found a workaround that works for me. I moved all the images to the
It worked in both I write a lot about Vue 3 and Nuxt 3 at enterprisevue.dev |
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
The text was updated successfully, but these errors were encountered: