flowbite: flowbite not working with NextJS
Issue
- We have installed flowbite to our react project by following recommended steps via npm. We added it to tailwind config etc
- The code is just not working properly. The effects (e.g. tooltip) do not show on hover. If we try to import it directly (via
import '@themesberg/flowbite';we get an errordocument not found. - Is it correct to inject the script just with
<script src="../path/to/@themesberg/flowbite/dist/flowbite.bundle.js"></script>? Is thepath/tojust a placeholder for another custom local path? - We are using NextJS. I think the issue could be related to SSR but I am not sure. Any pointers?
About this issue
- Original URL
- State: closed
- Created 3 years ago
- Comments: 76 (18 by maintainers)
Commits related to this issue
- fix: https://github.com/themesberg/flowbite/issues/51#issuecomment-1666715704 — committed to megmogmog1965/vulnerable-web-site by megmogmog1965 5 months ago
I found a workaround guys! Next.js has a Script tag that allows you to import scripts.
What I did was added this to my
_app.tsx/jsfile:After that I added this
to the built in Next.js
<Head>tag.Once I did that plus add
require('flowbite/plugin')to thetailwind.config.jsit worked!Hello guys, I started a while ago implementing a React library that contains all Flowbite components which will be SSR friendly and uses hooks instead of the vanilla Javascript logic. It is still in progress but if you want to give a hand all contributions are welcome 😄
I got the same problem right now! I’ll try to fix it.
If someone is still wondering how to make it work on latest Typescript project without flowbite-react components.
It worked for me, I had to follow these steps
In this case, you can go forward with just only base flowbite library.
Modify your
global.cssfileAdd path to flowbite module on
tailwind.config.js, something like this :Import flowbite library using ESModule import style statement on your
_app.tsxfileThen, try to put any component (e.g. Navbar) to see if it’s work.
PS: Sorry, I had to comment on this closed issue. It seems using native HTML syntax (base flowbite) is more suitable for me, especially with highly customize support. Actually, there is react component-ready library (flowbite-react) as a concern from other previous comments here.
Hey I don’t know if it helps you people, but I actually got a workaround on this… I’m still working on it but I got some javascript actions working with those lines.
on your
_app.jsadd:Looks like this is a NextJS specific problem. I’ll look into it better next week.
If you anyone finds a workaround, or more info on this, let me know.
in _app.tsx add this :
import { initFlowbite } from "flowbite";Edit :
Or in your every layout / component / page file. I prefer put this on layout.
import { initFlowbite } from "flowbite";It’s working for me
I add on my _app.js:
Hello, everyone!
Please check out the official React library for Flowbite: https://github.com/themesberg/flowbite-react
We’re close to a stable release. This should work with Next JS.
I’ll just give this a +1. Utilizing NextJS 12 and receiving this exact error when attempting to utilize.
@volp99 feel free to also fork the project as you may need to update the JS from Flowbite.
The main issue is with the event listeners not being registered as React uses a virtual DOM and the Flowbite script looks for those data attributes.
The other non-interactive elements should work regardless.
Hey everyone,
Lots of +1’s 😃
If you’re using the data attributes by copy-pasting the examples for the main docs then I advise you to either use the
useEffecthook or theonMountedlifecycle method and initialise the components that you need.Here’s an example documented for Nuxt which we will also later update for Next.js:
https://flowbite.com/docs/getting-started/nuxt-js/#data-attributes
Something like this:
Additionally, if you’re using Flowbite React please refer to that repository, there should be no problems with Next.js < 13 and for Next.js above 13 just use the
use client;code at the beginning of the page.Cheers, Zoltan
Now, it’s been a month but the issue has not been solved yet.
@bragamat Thanks for looking into this! This solution still doesnt work for me (e.g. the tooltip just does not appear on hover).
I’m working on a nextjs project. This solution works for me
Thank you for your response, @adriandelgg !! To clarify, is it necessary to use Next.js built-in upper case
Scriptinstead of just normal lower casescript, isn’t it?? I am going to try that one.Anybody can help me?? I am still struggling with this issue. I use Windows and VSCode. Installed libraries are followings ;
Then I tried followings ;
But unfortunately it didn’t work, which means datepicker didn’t show up.
We are aware of the problem and the best solution is to create a standalone React component library based on Flowbite, which we will shortly get started with right after shipping an update for the Flowbite website.
We’ll create contribution guidelines so anybody who would like to offer help, can do so.
Until then, there’s a temporary solution that can be used found by “@branko” from our Discord server:
https://discord.com/channels/902911619032576090/933850573999075398/934011104173113375
Here’s an extract of the text:
"ok so I did the following:
in tailwind config: in content i added:
'./node_modules/@themesberg/flowbite/**/*.js'and'./node_modules/@themesberg/flowbite/*/.js'and in plugins i addedrequire('@themesberg/flowbite/plugin')in my index.tsx i added
import('@themesberg/flowbite');inside auseEffect()with an empty dependency arrayin _document.tsx in Head i added
i’m sure some of these aren’t actually needed since i’m importing the CDN, but I was hitting my head against the wall still looking for a better solution for NextJS"
Sorry for the inconvenience, we are doing our best to provide support to all major front-end frameworks and libraries. I hope that this temporary solution is helpful enough until we launch the official library.
@volp99 Would be amazing if you could let me know in case you find a solution or other workaround 😃
Thanks for your reply,
I follow the steps as described above but I always get the same error. I try also to install again all the dependencies but nothing.
I’m going to find another way!