Astro with Bootstrap

I was trying to get Bootstrap to work in Astro. So far CSS is working nicely, but JS doesn't want to, gives me this error. My CSS import is the following:
import './public/bootstrap/dist/css/bootstrap.min.css';
import './public/bootstrap/dist/css/bootstrap.min.css';
Then I tried the same with JS:
import './public/bootstrap/dist/js/bootstrap.min.js';
import './public/bootstrap/dist/js/bootstrap.min.js';
As seen here: https://i.gyazo.com/83973f434cc7224703d16e1f8f5e526b.png It says it's trying to solve an issue where document doesn't exist. But honestly, I don't know how to understand the error, as I went to the file itself, with no luck figuring it out. Installed bootstrap through npm install bootstrap.
17 Replies
Myndi
MyndiOPā€¢3y ago
Also, Kevin's video is a bit outdated on the matter, as some of the ways he dealt with things are deprecated šŸ˜­
b1mind
b1mindā€¢3y ago
mmm could be cause you need to opt int for client side JS document does not exist in node https://docs.astro.build/en/core-concepts/astro-components/#client-side-scripts
b1mind
b1mindā€¢3y ago
GitHub
V5 Alert: Breaks SSR in Gatsby Ā· Issue #33722 Ā· twbs/bootstrap
Using bootstrap@^5.0.0-beta3 as ES6 modules in React/Gatsby.js In node window and document are undefined. There should be a check for these so they work in similar cases. https://github.com/twbs/bo...
b1mind
b1mindā€¢3y ago
Looks like the easy dirty fix is to just include the CDN in the head of your entry point html šŸ¤·ā€ā™‚ļø
Myndi
MyndiOPā€¢3y ago
I have them.
Myndi
MyndiOPā€¢3y ago
But they didn't work šŸ˜­ That's why I tried doing npm.
b1mind
b1mindā€¢3y ago
whats your entry point? I have not build a astro project in ages, I'm assuming its like app.html
Myndi
MyndiOPā€¢3y ago
I'm adding this in: Layout.astro.
Myndi
MyndiOPā€¢3y ago
Pretty much referencing Kevin's video. On where he linked a style.css there this way.
b1mind
b1mindā€¢3y ago
Assuming you need it in index.html Or again if you put it in layout you need to run a check if (browser) then load. From the issues I linked
const bootstrap = typeof window !== `undefined` && import("bootstrap")
const bootstrap = typeof window !== `undefined` && import("bootstrap")
https://getbootstrap.com/docs/5.2/getting-started/vite/ You could try bringing it via Vite too idk how well that works in Astro
Myndi
MyndiOPā€¢3y ago
I will keep trying solutions. Thanks for the help. now it's time to pressure Kevin to do an updated Astro video
b1mind
b1mindā€¢3y ago
Yea tricky one when its not a ESM module He wouldn't use bootstrap though eh šŸ˜„ Another hacky solution I would try is just copy/pasta the min.js file intp Public and load it as a static asset
Myndi
MyndiOPā€¢3y ago
That's fine as well, any example or reference works. The problem is deprecated functions šŸ˜­
b1mind
b1mindā€¢3y ago
Ah gotcha
Myndi
MyndiOPā€¢3y ago
That's what I did thumbsUP
Unknown User
Unknown Userā€¢3y ago
Message Not Public
Sign In & Join Server To View
Myndi
MyndiOPā€¢3y ago
Import it on the layouts instead of the components.
Want results from more Discord servers?
Add your server