tailwindcss is not reflected in rails app

Hi!
I am Japanese.
My English is poor, so I apologize if it is not clear.
This is a continuation of the question here.

I am using tailwindcss and daisyUI for the front end.
However, the app deployed in the production environment does not reflect and layout is broken.
I don’t know the cause.
Are there any codes that should be improved?
I have included the code that I believe is relevant to tailwindcss and daisyUI.

Gemfile :

gem 'cssbundling-rails'
gem "tailwindcss-rails"

config/tailwind.config.js :

module.exports = {
  content: [
    "../myapp/**/*.{html.erb, html, js}",
  ],
  theme: {
      extend: {
        colors: {
        },
      },
    },
  plugins: [
    require('daisyui'),
  ],
  daisyui: {
    themes: [
      "bumblebee"
    ],
  },
}

package.json :

{
  "name": "sample_app",
  "version": "1.0.0",
  "main": "index.js",
  "repository": "xxxxxxxxxxxxxxxxxxxxxxxxx",
  "author": "xxxxxxxxxxxxxxxxxxxxxxxxx",
  "license": "MIT",
  "dependencies": {
    "@hotwired/stimulus": "^3.2.1",
    "tom-select": "^2.2.2"
  },
  "scripts": {
    "build:css": "tailwindcss -i ./app/assets/stylesheets/application.tailwind.css -o ./app/assets/builds/tailwind.css"
  },
  "packageManager": "yarn@1.22.19"
}

app/assets/stylesheets/application.tailwind.css

@tailwind base;
@tailwind components;
@tailwind utilities;

@layer components {
  .btn {
    @apply py-2 px-4 bg-orange ;
  }
}

app/views/layouts/application.html.erb

<!DOCTYPE html>
<html data-theme="bumblebee">
  <head>
    <%= stylesheet_link_tag "tailwind", "inter-font", "data-turbo-track": "reload" %>
    <%= stylesheet_link_tag "https://cdn.jsdelivr.net/npm/daisyui@2.14.1/dist/full.css" %>
  </head>
</html>

This one I can’t explain. Everything looks right, but it is almost as if you aren’t running assets:precompile, but I know from your previous question that you are.

Does this work on your machine?

bin/rails assets:precompile
bin/rails s -e production

You previously mentioned docker compose. If you are not running directly on your machine but rather in a dockerfile, you can generate a docker-compose.yml file that you can use:

bin/rails generate dockerfile --compose
docker compose up --build

This one I can’t explain.

Yes, that’s right. Thank you.

Sorry, I am not sure I understand your answer due to my lack of understanding.
I have a docker container in the development environment locally that I start every time with docker-compose up -d.
As a test, I started it with docker-compose up -d and then typed the command docker-compose exec api bin/rails assets:precompile and the following log appeared.

hoge@MacBook-Air-2 sample_app % docker-compose exec api bin/rails assets:precompile
yarn install v1.22.19
warning package-lock.json found. Your project contains lock files generated by tools other than Yarn. It is advised not to mix package managers in order to avoid resolution inconsistencies caused by unsynchronized lock files. To clear this warning, remove package-lock.json.
[1/4] Resolving packages...
success Already up-to-date.
Done in 0.08s.
yarn run v1.22.19
$ tailwindcss -i ./app/assets/stylesheets/application.tailwind.css -o ./app/assets/builds/tailwind.css
["/usr/local/bundle/gems/tailwindcss-rails-2.0.24-aarch64-linux/exe/aarch64-linux/tailwindcss", "-i", "./app/assets/stylesheets/application.tailwind.css", "-o", "./app/assets/builds/tailwind.css"]
Browserslist: caniuse-lite is outdated. Please run:
  npx update-browserslist-db@latest
  Why you should do it regularly: https://github.com/browserslist/update-db#readme

Rebuilding...

warn - No utility classes were detected in your source files. If this is unexpected, double-check the `content` option in your Tailwind CSS configuration.
warn - https://tailwindcss.com/docs/content-configuration

Done in 139ms.
Done in 0.87s.
Browserslist: caniuse-lite is outdated. Please run:
  npx update-browserslist-db@latest
  Why you should do it regularly: https://github.com/browserslist/update-db#readme

Rebuilding...
Error: Cannot find module 'daisyui'
Require stack:
- /myapp/config/tailwind.config.js
- /snapshot/tailwindcss/lib/cli/build/plugin.js
- /snapshot/tailwindcss/lib/cli/build/index.js
- /snapshot/tailwindcss/lib/cli/index.js
- /snapshot/tailwindcss/lib/cli.js
- /snapshot/tailwindcss/standalone-cli/standalone.js
1) If you want to compile the package/file into executable, please pay attention to compilation warnings and specify a literal in 'require' call. 2) If you don't want to compile the package/file into executable and want to 'require' it from filesystem (likely plugin), specify an absolute path in 'require' call using process.cwd() or process.execPath.
    at Function.Module._resolveFilename (node:internal/modules/cjs/loader:933:15)
    at Function._resolveFilename (pkg/prelude/bootstrap.js:1955:46)
    at Function.Module._load (node:internal/modules/cjs/loader:778:27)
    at Module.require (node:internal/modules/cjs/loader:1005:19)
    at Module.require (pkg/prelude/bootstrap.js:1855:31)
    at Module.require (/snapshot/tailwindcss/standalone-cli/standalone.js:26:22)
    at require (node:internal/modules/cjs/helpers:102:18)
    at Object.<anonymous> (/myapp/config/tailwind.config.js:12:5)
    at Module._compile (node:internal/modules/cjs/loader:1105:14)
    at Module._compile (pkg/prelude/bootstrap.js:1894:32) {
  code: 'MODULE_NOT_FOUND',
  requireStack: [
    '/myapp/config/tailwind.config.js',
    '/snapshot/tailwindcss/lib/cli/build/plugin.js',
    '/snapshot/tailwindcss/lib/cli/build/index.js',
    '/snapshot/tailwindcss/lib/cli/index.js',
    '/snapshot/tailwindcss/lib/cli.js',
    '/snapshot/tailwindcss/standalone-cli/standalone.js'
  ],
  pkg: true
}

The local browser also had a broken layout, as did the application deployed to the production environment. Moreover, it is still collapsed and will not revert back to its original state.
Since it says `Error: Cannot find module ‘daisyui’', I think daisyUI is not loaded correctly.
Previously, entering the docker-compose exec api bin/rails generate dockerfile command also created a dockerfile, but at the same time the yarn-1.22.19.cjs file was automatically created.
Is the yarn-1.22.19.cjs file doing something wrong?

I don’t see daisyui in your package.json file.

Try running yarn add daisyui, verifying that it is in package.json after this command completes, and then redeploying.

I performed yarn add daisyui, confirmed, and redeployed.
However, I get the following error

=> ERROR [build  6/10] RUN yarn install --frozen-lockfile                                                      7.2s
------
 > [build  6/10] RUN yarn install --frozen-lockfile:
#16 0.927 yarn install v1.22.19
#16 1.011 warning package-lock.json found. Your project contains lock files generated by tools other than Yarn. It is advised not to mix package managers in order to avoid resolution inconsistencies caused by unsynchronized lock files. To clear this warning, remove package-lock.json.
#16 1.014 [1/4] Resolving packages...
#16 1.079 [2/4] Fetching packages...
#16 6.983 error tailwindcss@3.3.2: The engine "node" is incompatible with this module. Expected version ">=14.0.0". Got "12.22.12"
#16 6.989 error Found incompatible module.
#16 6.990 info Visit https://yarnpkg.com/en/docs/cli/install for documentation about this command.
------
Error: failed to fetch an image or build from source: error building: executor failed running [/bin/sh -c yarn install --frozen-lockfile]: exit code: 1

The related files are listed below.
package.json :

{
  "name": "sample_app",
  "version": "1.0.0",
  "main": "index.js",
  "repository": "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
  "author": "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
  "license": "MIT",
  "dependencies": {
    "@hotwired/stimulus": "^3.2.1",
    "daisyui": "^2.51.6",
    "tom-select": "^2.2.2"
  },
  "scripts": {
    "build:css": "tailwindcss -i ./app/assets/stylesheets/application.tailwind.css -o ./app/assets/builds/tailwind.css"
  },
  "packageManager": "yarn@1.22.19"
}

node-version :

19.0.0

The node-version was originally ‘12.22.12’ but was changed to 19.0.0 because yarn add daisyui could not be done.
Does the change to 19.0.0 not reflect the change to 19.0.0?
If you have any suggestions for improvement, please let us know.

Look in your Dockerfile for a line that starts with ARG NODE_VERSION=. Does it still say 12.22.12? If so, change it.

Changed to ARG NODE_VERSION=19.0.0!

# syntax = docker/dockerfile:1

# Make sure RUBY_VERSION matches the Ruby version in .ruby-version and Gemfile
ARG RUBY_VERSION=3.1.2
FROM ruby:$RUBY_VERSION-slim as base

# Rails app lives here
WORKDIR /rails

# Set production environment
ENV RAILS_ENV="production" \
    BUNDLE_WITHOUT="development:test" \
    BUNDLE_DEPLOYMENT="1"

# Update gems and bundler
RUN gem update --system --no-document && \
    gem install -N bundler


# Throw-away build stage to reduce size of final image
FROM base as build

# Install packages needed to build gems and node modules
RUN apt-get update -qq && \
    apt-get install --no-install-recommends -y build-essential curl libpq-dev node-gyp pkg-config python-is-python3

# Install JavaScript dependencies
ARG NODE_VERSION=19.0.0
ARG YARN_VERSION=1.22.19
ENV PATH=/usr/local/node/bin:$PATH
RUN curl -sL https://github.com/nodenv/node-build/archive/master.tar.gz | tar xz -C /tmp/ && \
    /tmp/node-build-master/bin/node-build "${NODE_VERSION}" /usr/local/node && \
    npm install -g yarn@$YARN_VERSION && \
    rm -rf /tmp/node-build-master

# Install application gems
COPY --link Gemfile Gemfile.lock ./
RUN bundle install && \
    bundle exec bootsnap precompile --gemfile && \
    rm -rf ~/.bundle/ $BUNDLE_PATH/ruby/*/cache $BUNDLE_PATH/ruby/*/bundler/gems/*/.git

# Install node modules
COPY --link package.json package-lock.json yarn.lock ./
RUN yarn install --frozen-lockfile

# Copy application code
COPY --link . .

# Precompile bootsnap code for faster boot times
RUN bundle exec bootsnap precompile app/ lib/

# Precompiling assets for production without requiring secret RAILS_MASTER_KEY
RUN SECRET_KEY_BASE=DUMMY ./bin/rails assets:precompile
RUN rm -f /rails/tmp/pids/server.pid

# Final stage for app image
FROM base

# Install packages needed for deployment
RUN apt-get update -qq && \
    apt-get install --no-install-recommends -y postgresql-client && \
    rm -rf /var/lib/apt/lists /var/cache/apt/archives

# Run and own the application files as a non-root user for security
RUN useradd rails --home /rails --shell /bin/bash
USER rails:rails

# Copy built artifacts: gems, application
COPY --from=build /usr/local/bundle /usr/local/bundle
COPY --from=build --chown=rails:rails /rails /rails

# Deployment options
ENV RAILS_LOG_TO_STDOUT="1" \
    RAILS_SERVE_STATIC_FILES="true"

# Entrypoint prepares the database.
ENTRYPOINT ["/rails/bin/docker-entrypoint"]

# Start the server by default, this can be overwritten at runtime
EXPOSE 3000
CMD ["./bin/rails", "server"]

When deployed, the following error occurred

=> ERROR [build  9/10] RUN SECRET_KEY_BASE=DUMMY ./bin/rails assets:precompile                                 5.2s
------
 > [build  9/10] RUN SECRET_KEY_BASE=DUMMY ./bin/rails assets:precompile:
#19 2.312 yarn install v1.22.19
#19 2.367 warning package-lock.json found. Your project contains lock files generated by tools other than Yarn. It is advised not to mix package managers in order to avoid resolution inconsistencies caused by unsynchronized lock files. To clear this warning, remove package-lock.json.
#19 2.374 [1/4] Resolving packages...
#19 2.428 success Already up-to-date.
#19 2.435 Done in 0.13s.
#19 2.816 yarn run v1.22.19
#19 2.868 $ tailwindcss -i ./app/assets/stylesheets/application.tailwind.css -o ./app/assets/builds/tailwind.css
#19 3.322
#19 3.322 Rebuilding...
#19 3.431
#19 3.431 warn - No utility classes were detected in your source files. If this is unexpected, double-check the `content` option in your Tailwind CSS configuration.
#19 3.431 warn - https://tailwindcss.com/docs/content-configuration
#19 3.468
#19 3.468 Done in 195ms.
#19 3.488 Done in 0.68s.
#19 4.366 Browserslist: caniuse-lite is outdated. Please run:
#19 4.366   npx update-browserslist-db@latest
#19 4.366   Why you should do it regularly: https://github.com/browserslist/update-db#readme
#19 4.431
#19 4.431 Rebuilding...
#19 4.610
#19 4.610 🌼 daisyUI components 2.51.6  https://daisyui.com
#19 4.868   ✔︎ Including:  base, components, 1 themes, utilities
#19 4.868   ❤︎ Support daisyUI:  https://opencollective.com/daisyui
#19 4.868
#19 4.891
#19 4.891 warn - No utility classes were detected in your source files. If this is unexpected, double-check the `content` option in your Tailwind CSS configuration.
#19 4.891 warn - https://tailwindcss.com/docs/content-configuration
#19 4.924
#19 4.924 Done in 570ms.
#19 5.090 rails aborted!
#19 5.091 SassC::SyntaxError: Error: Function hsla is missing argument $saturation.
#19 5.091         on line 424 of stdin
#19 5.091 >>   background-color: hsla(var(--b1) / var(--tw-bg-opacity, 1));
#19 5.091
#19 5.091    --------------------^
#19 5.091 /rails/vendor/bundle/ruby/3.1.0/gems/sassc-2.4.0/lib/sassc/engine.rb:50:in `render'
#19 5.091 /rails/vendor/bundle/ruby/3.1.0/gems/sassc-rails-2.1.2/lib/sassc/rails/compressor.rb:29:in `call'
#19 5.091 /rails/vendor/bundle/ruby/3.1.0/gems/sprockets-4.2.0/lib/sprockets/sass_compressor.rb:30:in `call'
#19 5.091 /rails/vendor/bundle/ruby/3.1.0/gems/sprockets-4.2.0/lib/sprockets/processor_utils.rb:84:in `call_processor'
#19 5.091 /rails/vendor/bundle/ruby/3.1.0/gems/sprockets-4.2.0/lib/sprockets/processor_utils.rb:66:in `block in call_processors'
#19 5.091 /rails/vendor/bundle/ruby/3.1.0/gems/sprockets-4.2.0/lib/sprockets/processor_utils.rb:65:in `reverse_each'
#19 5.091 /rails/vendor/bundle/ruby/3.1.0/gems/sprockets-4.2.0/lib/sprockets/processor_utils.rb:65:in `call_processors'
#19 5.091 /rails/vendor/bundle/ruby/3.1.0/gems/sprockets-4.2.0/lib/sprockets/loader.rb:182:in `load_from_unloaded'
#19 5.091 /rails/vendor/bundle/ruby/3.1.0/gems/sprockets-4.2.0/lib/sprockets/loader.rb:59:in `block in load'
#19 5.091 /rails/vendor/bundle/ruby/3.1.0/gems/sprockets-4.2.0/lib/sprockets/loader.rb:337:in `fetch_asset_from_dependency_cache'
#19 5.091 /rails/vendor/bundle/ruby/3.1.0/gems/sprockets-4.2.0/lib/sprockets/loader.rb:43:in `load'
#19 5.091 /rails/vendor/bundle/ruby/3.1.0/gems/sprockets-4.2.0/lib/sprockets/cached_environment.rb:44:in `block in load'
#19 5.091 /rails/vendor/bundle/ruby/3.1.0/gems/concurrent-ruby-1.2.2/lib/concurrent-ruby/concurrent/map.rb:207:in `block in fetch_or_store'
#19 5.091 /rails/vendor/bundle/ruby/3.1.0/gems/concurrent-ruby-1.2.2/lib/concurrent-ruby/concurrent/map.rb:187:in `fetch'
#19 5.091 /rails/vendor/bundle/ruby/3.1.0/gems/concurrent-ruby-1.2.2/lib/concurrent-ruby/concurrent/map.rb:206:in `fetch_or_store'
#19 5.091 /rails/vendor/bundle/ruby/3.1.0/gems/sprockets-4.2.0/lib/sprockets/cached_environment.rb:44:in `load'
#19 5.091 /rails/vendor/bundle/ruby/3.1.0/gems/sprockets-4.2.0/lib/sprockets/bundle.rb:32:in `block in call'
#19 5.091 /rails/vendor/bundle/ruby/3.1.0/gems/sprockets-4.2.0/lib/sprockets/bundle.rb:31:in `call'
#19 5.091 /rails/vendor/bundle/ruby/3.1.0/gems/sprockets-4.2.0/lib/sprockets/processor_utils.rb:84:in `call_processor'
#19 5.091 /rails/vendor/bundle/ruby/3.1.0/gems/sprockets-4.2.0/lib/sprockets/processor_utils.rb:66:in `block in call_processors'
#19 5.091 /rails/vendor/bundle/ruby/3.1.0/gems/sprockets-4.2.0/lib/sprockets/processor_utils.rb:65:in `reverse_each'
#19 5.091 /rails/vendor/bundle/ruby/3.1.0/gems/sprockets-4.2.0/lib/sprockets/processor_utils.rb:65:in `call_processors'
#19 5.091 /rails/vendor/bundle/ruby/3.1.0/gems/sprockets-4.2.0/lib/sprockets/loader.rb:182:in `load_from_unloaded'
#19 5.091 /rails/vendor/bundle/ruby/3.1.0/gems/sprockets-4.2.0/lib/sprockets/loader.rb:59:in `block in load'
#19 5.091 /rails/vendor/bundle/ruby/3.1.0/gems/sprockets-4.2.0/lib/sprockets/loader.rb:337:in `fetch_asset_from_dependency_cache'
#19 5.091 /rails/vendor/bundle/ruby/3.1.0/gems/sprockets-4.2.0/lib/sprockets/loader.rb:43:in `load'
#19 5.091 /rails/vendor/bundle/ruby/3.1.0/gems/sprockets-4.2.0/lib/sprockets/cached_environment.rb:44:in `block in load'
#19 5.091 /rails/vendor/bundle/ruby/3.1.0/gems/concurrent-ruby-1.2.2/lib/concurrent-ruby/concurrent/map.rb:207:in `block in fetch_or_store'
#19 5.091 /rails/vendor/bundle/ruby/3.1.0/gems/concurrent-ruby-1.2.2/lib/concurrent-ruby/concurrent/map.rb:187:in `fetch'
#19 5.091 /rails/vendor/bundle/ruby/3.1.0/gems/concurrent-ruby-1.2.2/lib/concurrent-ruby/concurrent/map.rb:206:in `fetch_or_store'
#19 5.091 /rails/vendor/bundle/ruby/3.1.0/gems/sprockets-4.2.0/lib/sprockets/cached_environment.rb:44:in `load'
#19 5.091 /rails/vendor/bundle/ruby/3.1.0/gems/sprockets-4.2.0/lib/sprockets/base.rb:81:in `find_asset'
#19 5.091 /rails/vendor/bundle/ruby/3.1.0/gems/sprockets-4.2.0/lib/sprockets/base.rb:88:in `find_all_linked_assets'
#19 5.091 /rails/vendor/bundle/ruby/3.1.0/gems/sprockets-4.2.0/lib/sprockets/manifest.rb:125:in `each'
#19 5.091 /rails/vendor/bundle/ruby/3.1.0/gems/sprockets-4.2.0/lib/sprockets/manifest.rb:125:in `to_a'
#19 5.091 /rails/vendor/bundle/ruby/3.1.0/gems/sprockets-4.2.0/lib/sprockets/manifest.rb:125:in `block (2 levels) in find'
#19 5.091 /rails/vendor/bundle/ruby/3.1.0/gems/concurrent-ruby-1.2.2/lib/concurrent-ruby/concurrent/executor/safe_task_executor.rb:24:in `block in execute'
#19 5.091 /rails/vendor/bundle/ruby/3.1.0/gems/concurrent-ruby-1.2.2/lib/concurrent-ruby/concurrent/synchronization/mutex_lockable_object.rb:48:in `block in synchronize'
#19 5.091 /rails/vendor/bundle/ruby/3.1.0/gems/concurrent-ruby-1.2.2/lib/concurrent-ruby/concurrent/synchronization/mutex_lockable_object.rb:48:in `synchronize'
#19 5.091 /rails/vendor/bundle/ruby/3.1.0/gems/concurrent-ruby-1.2.2/lib/concurrent-ruby/concurrent/synchronization/mutex_lockable_object.rb:48:in `synchronize'
#19 5.091 /rails/vendor/bundle/ruby/3.1.0/gems/concurrent-ruby-1.2.2/lib/concurrent-ruby/concurrent/executor/safe_task_executor.rb:22:in `execute'
#19 5.091 /rails/vendor/bundle/ruby/3.1.0/gems/concurrent-ruby-1.2.2/lib/concurrent-ruby/concurrent/promise.rb:564:in `block in realize'
#19 5.091 /rails/vendor/bundle/ruby/3.1.0/gems/concurrent-ruby-1.2.2/lib/concurrent-ruby/concurrent/executor/ruby_thread_pool_executor.rb:352:in `run_task'
#19 5.091 /rails/vendor/bundle/ruby/3.1.0/gems/concurrent-ruby-1.2.2/lib/concurrent-ruby/concurrent/executor/ruby_thread_pool_executor.rb:343:in `block (3 levels) in create_worker'
#19 5.091 /rails/vendor/bundle/ruby/3.1.0/gems/concurrent-ruby-1.2.2/lib/concurrent-ruby/concurrent/executor/ruby_thread_pool_executor.rb:334:in `loop'
#19 5.091 /rails/vendor/bundle/ruby/3.1.0/gems/concurrent-ruby-1.2.2/lib/concurrent-ruby/concurrent/executor/ruby_thread_pool_executor.rb:334:in `block (2 levels) in create_worker'
#19 5.091 /rails/vendor/bundle/ruby/3.1.0/gems/concurrent-ruby-1.2.2/lib/concurrent-ruby/concurrent/executor/ruby_thread_pool_executor.rb:333:in `catch'
#19 5.091 /rails/vendor/bundle/ruby/3.1.0/gems/concurrent-ruby-1.2.2/lib/concurrent-ruby/concurrent/executor/ruby_thread_pool_executor.rb:333:in `block in create_worker'
#19 5.091 Tasks: TOP => assets:precompile
#19 5.092 (See full trace by running task with --trace)
------
Error: failed to fetch an image or build from source: error building: executor failed running [/bin/sh -c SECRET_KEY_BASE=DUMMY ./bin/rails assets:precompile]: exit code: 1

To resolve the error, I added config.assets.css_compressor = nil to production.rb, which resolved the error and deployed successfully.
However, the layout is still corrupted.

Can you think of any other possible causes?

I have a .yran/releases/yarn-1.22.19.cjs file and a Dockerfile of

ARG NODE_VERSION=19.0.0
ARG YARN_VERSION=1.22.19
ENV PATH=/usr/local/node/bin:$PATH
RUN curl -sL https://github.com/nodenv/node-build/archive/master.tar.gz | tar xz -C /tmp/ && \
    /tmp/node-build-master/bin/node-build "${NODE_VERSION}" /usr/local/node && \
    npm install -g yarn@$YARN_VERSION && \
    rm -rf /tmp/node-build-master

is having a bad influence.This is just my guess.

.yran/releases/yarn-1.22.19.cjs is the file created when you enter the docker-compose exec api bin/rails generate dockerfile command.

The docker-compose exec api bin/rails assets:precompile command is causing the layout to be broken in my local environment. I would like to solve this problem too, but I think .yran/releases/yarn-1.22.19.cjs is having a bad influence.

Sorry for the long sentence.
Maybe my writing is bullshit and I don’t get the message, but please let me know.

You are definitely getting into areas that I’m not familiar with, but I did manage to find build - Rails 7 asset pipeline SassC::SyntaxError with Tailwind - Stack Overflow and GitHub - rails/tailwindcss-rails that might be helpful.

I see.
I will try my best to refer to the article.
Thank you very much.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.