question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

require is not defined

See original GitHub issue

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)

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:1
  • Comments:29 (5 by maintainers)

github_iconTop GitHub Comments

46reactions
danielroecommented, Nov 16, 2021

If you are using vite then require will not work; it’s webpack-only. Instead, just use:

<template>
  <div class="app">
    <img src="~/assets/image.png">
  </div>
</template>
21reactions
juancarlos-ctgcommented, May 9, 2022

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.js

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 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,

<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

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found