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.

"document is not defined" when using bootstrap js

See original GitHub issue

So I have this component which uses collapse.js of bootstrap,

<script lang="ts" setup>
import { onMounted } from "@vue/runtime-core"
import Collapse from "bootstrap/js/dist/collapse"

onMounted(() => {
  new Collapse(document.body)
})
</script>

<template>
  <div>
    <slot></slot>
  </div>
</template>

I get this error: ReferenceError: document is not defined. On nuxt, you could do,

onMounted(() => {
  if(process.client) {
    new Collapse(document.body)
  }
})

How do I do the same?

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:12 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
brilloutcommented, Sep 22, 2021

Makes sense.

0reactions
axelthatcommented, Sep 22, 2021

Yeah but that will include the whole bootstrap js file. I only needed some components. So did the lazy loading js way.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Bootstrap throws error 'ReferenceError: document is not defined'
If we directly import JS like CSS we will run into error which would say window and document is not defined. This is...
Read more >
Document is not defined Javascript - Stack Overflow
You're getting this error because you're running JavaScript in the server-side Node environment where the DOM ( document , window , etc) isn't ......
Read more >
ReferenceError: document is not defined in JavaScript
To solve the"ReferenceError: document is not defined" error, make sure to only use the document global variable on the browser. The variable relates...
Read more >
How to solve the document is not defined error - Flavio Copes
Here's how to fix the “referenceerror: document is not defined” error that you might have in Node.js or with a tool like Next.js....
Read more >
Fixing Next js "ReferenceError: document is not defined"
The error is thrown because document is only available inside the browser and not on the server. Next js executes this code on...
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