require is not defined
See original GitHub issueEnvironment
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)
Issue Analytics
- State:
- Created 2 years ago
- Reactions:1
- Comments:29 (5 by maintainers)
Top Results From Across the Web
Client on Node.js: Uncaught ReferenceError: require is not ...
However, require is not defined on the client side, and it throws an error of the form Uncaught ReferenceError: require is not defined...
Read more >ReferenceError: require is not defined in JavaScript - Stack Diary
The "ReferenceError: require is not defined" error occurs when the require function is used in a JavaScript file that is intended to be ......
Read more >How to fix "require is not defined" in JavaScript / Node.js?
In our case, the error message is “require is not defined” which indicates the problem is with the variable or method named “require”....
Read more >How To Fix ReferenceError require is not defined in JavaScript
The Solution. Your ReferenceError: require is not defined likely has one of two causes: You tried using require in a browser environment ......
Read more >Fix "require is not defined" in JavaScript/Node - Stack Abuse
You try to use the require statement in the browser · You try to use require in Node when ES modules are specified...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
If you are using vite then
require
will 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.png
where 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
script
tag.For example,
Again, this is for Nuxt 3