kingdomcoder
How to get DaisyUI and Phoenix to work?
I just had my first frustrating experience working in the Elixir ecosystem.
Is there a reason it is so hard to get DaisyUI deployed with my Phoenix app on Fly.io (and is there a way to improve it?)
I’m happy to contribute to solving the problem. I just need to understand what makes adding a plugin to the Tailwind hex package so difficult.
PS: As a side note, I’ll be happy to know that I’m not the only one who has been frustrated by this problem. Please, chime in if you’ve faced this too.
Marked As Solved
kingdomcoder
Finding the article you referred to gave me the missing piece.
Here are the relevant steps. I assume you have a Phoenix app setup and npm installed on you device.
- Install and setup the Elixir Tailwind library. The README of the library is a great resource.
- Publish your app to fly using flyctl. This post is a great guide. Just note that the
flycommands in the post might need to be replaced withflyctlcommands. - On your local machine,
cdinto your project’sassetsfolder and runnpm init. - From within your
assetsfolder, follow the DaisyUI installation steps. - Update the
Dockerfilegenerated during the early fly.io deployment. Perform the following
a. Ensure theCOPY lib libcommand comes before theRUN mix assets.deploycommnd
b. Include this commandRUN npm --prefix ./assets ci --progress=false --no-audit --loglevel=errorin theDockerfile. This should also come before theRUN mix assets.deploycommand
Expectedly, you should be good to go and your app should properly deploy to fly.io ![]()
Also Liked
arcanemachine
You are overcomplicating things. I just tested this with a fresh Phoenix 1.7.7 project:
Setup DaisyUI
-
Create project:
mix phx.new your_project
-
Initialize an npm project in the directory
assets/:cd assets && npm init -y
-
Add npm dependency
daisyuiin the directoryassets/:cd assets && npm i -D daisyui@latest
-
Add daisyUI to Tailwind plugins list (in
assets/tailwind.config.js):require("daisyUI")
plugins: [
+ require('daisyui'),
require("@tailwindcss/forms"),
- DaisyUI is now installed! Let’s try an example component:
lib/your_project_web/controllers/page_html/home.html.heex
<h1 class="text-brand mt-10 flex items-center text-sm font-semibold leading-6">
Phoenix Framework
<small class="bg-brand/5 text-[0.8125rem] ml-3 rounded-full px-2 font-medium leading-6">
v<%= Application.spec(:phoenix, :vsn) %>
</small>
</h1>
+ <div class="btn btn-primary">
+ Hello daisyUI!
+ </div>
<p class="text-[2rem] mt-4 font-semibold leading-10 tracking-tighter text-zinc-900">
Peace of mind from prototype to production.
</p>
It worked (for me, at least
)!
At this point, daisyUI is installed and everything should be working as expected.
Now for the deployment:
Deploy to Fly.io
-
Create Fly.io app:
- ‘flyctl launch’
-
Fix issues with the default Dockerfile generated when we ran
fly launch:- Use
apt-getto installnodejsandnpm - Install our project’s
npmdependencies
- Use
FROM ${BUILDER_IMAGE} as builder
# install build dependencies
-RUN apt-get update -y && apt-get install -y build-essential git \
+RUN apt-get update -y && apt-get install -y build-essential git nodejs npm \
&& apt-get clean && rm -f /var/lib/apt/lists/*_*
# ...
COPY priv priv
COPY lib lib
COPY assets assets
+# install npm dependencies
+RUN cd assets && npm install
+
# compile assets
RUN mix assets.deploy
Bonus: Prevent daisyUI from spamming the console when tailwind.config.js is modified
By default, daisyUI will print a message like this to the console whenever tailwind.config.js is modified:
🌼 daisyUI 3.5.0 https://daisyui.com
╰╮
╰─ ✔︎ [ 2 ] themes are enabled. You can add more themes or make your own theme:
https://daisyui.com/docs/themes
❤︎ Support daisyUI: https://opencollective.com/daisyui
That’s annoying. Let’s disable that:
assets/tailwind.config.js
module.exports = {
content: ["./js/**/*.js", "../lib/*_web.ex", "../lib/*_web/**/*.*ex"],
+ daisyui: {
+ logs: false,
+ },
plugins: [
Bonus: Remove tailwind/forms to avoid conflicts with daisyUI form components
The Tailwind forms package will cause conflicts with daisyUI forms. Let’s get rid of it:
assets/tailwind.config.js
plugins: [
require("daisyui"),
- require("@tailwindcss/forms"),
require("@tailwindcss/typography"),
benwilson512
Hey @kingdomcoder, I’m sorry you’ve had a frustrating experience. While I have not used DaisyUI myself, I imagine others who want to help would find it easier if you described what made this hard for you, or what problems you ran into that require help.
cvkmohan
I just realized that tailwindcss-cli is working with 3rd party plugins also in the latest version. That would mean that the best way to integrate daisyui into phoenix application is:
- create a new phoenix application.
- Use phoenixframework/tailwind: An installer for tailwind (github.com) to integrate tailwindcss into the project
cd assetsyarn add daisyui- In your
tailwind.config.jsfile add the lineplugins: [require("daisyui")],
That is it. There is no 6th step. ![]()
Last Post!
cvkmohan
@PJUllrich - I guess npm install daisyui is the missing step - either in development or in production - wherever the error happens.
The above steps are still working for me. For fly deployment the above @arcanemachine steps are sufficient.
Popular in Questions
Other popular topics
Latest Phoenix Threads
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #supervisor
- #elixirconf-us
- #advent-of-code
- #distillery
- #processes
- #forms
- #api
- #metaprogramming
- #hex
- #security









