How to integrate Bootstrap stylesheets into Rails 6 using Webpacker and the app/javascript directory

I'm a passionate software developer.
Search for a command to run...

I'm a passionate software developer.
No comments yet. Be the first to comment.
Components Introduction In order to truly understand props in React, we first need to be clear on what components are. A component is a reusable piece of the user interface that encapsulates structure, styling, and behavior. Instead of repeating the ...

Introduction Context By default, Codespace is using the GITHUB_TOKEN env var (the “Codespaces token”), which can’t create repos. It’s necessary to authenticate gh with a token/account that has the right scopes, or create the repo on the web first. Ob...

Solution Here it is: git commit --allow-empty-message -m "" Explanation To create a Git commit without a message, or with an empty message, you can use the --allow-empty-message flag with the git commit command. git commit --allow-empty-message -m "...

Steps Cleanup phase This phase’s goal is to removes existing containers, volumes, networks and images Stop and remove volumes docker compose down --volumes --remove-orphans The previous command stops and removes containers, networks, and default volu...

Steps Stop the service It’s not mandatory, but before uninstalling, it's a good practice to stop any running Docker services. sudo systemctl stop docker Remove docker packages It’s used to completely and forcefully remove a list of specified packag...

In Rails 6, there was a shift in how JavaScript and CSS assets are managed in the default application setup. This change was made to align with modern front-end development practices and the increasing popularity of JavaScript frameworks like React, Vue.js, and Angular.
In the context of Bootstrap 5 (or any modern CSS framework), the app/javascript directory is used for JavaScript modules and assets, while the app/assets directory is typically reserved for the asset pipeline, which is a legacy approach for managing assets in older versions of Rails.
This change was part of Rails' move toward using Webpacker, which is a tool for managing JavaScript, CSS, and other assets in Rails applications.
mkdir app/javascript/stylesheets
Output

echo > app/javascript/stylesheets/application.scss
Output

So far, if you try to access any view page and check the terminal logs, you'll see:

Inside the main view file, which is app/views/layouts/application.html.erb, your file should look something like the following:

The previous command uses the tylesheet_pack_tag, which is a Rails helper method provided by Webpacker. It's used to include a stylesheet bundle that was defined in your Webpacker configuration. This method takes the name of the pack (or bundle) as an argument.
By convention, the main stylesheet for your application is often named 'application'
In this case, we use 'application' because is the name of the stylesheet pack that we want to include, but we can have other named packs for different parts of our application.
'data-turbolinks-track': 'reload': This is an optional HTML attribute that's added to the link tag for the stylesheet. It's used in conjunction with Turbolinks, which is a JavaScript library used in Rails to speed up page transitions. The data-turbolinks-track attribute with a value of 'reload' ensures that the stylesheet is reloaded when navigating between pages using Turbolinks.
The file is located in app/javascript/packs/application.js.

Inside the main javascript file, which is app/javascript/packs/application.js, add the following libraries:
# app/javascript/packs/application.js
import 'bootstrap/dist/js/bootstrap'
import 'bootstrap/dist/css/bootstrap'
import 'stylesheets/application'
The app/javascript/packs/application.jsfile would look like this:
// This file is automatically compiled by Webpack, along with any other files
// present in this directory. You're encouraged to place your actual application logic in
// a relevant structure within app/javascript and only use these pack files to reference
// that code so it'll be compiled.
import Rails from "@rails/ujs"
import Turbolinks from "turbolinks"
import * as ActiveStorage from "@rails/activestorage"
import "channels"
import 'bootstrap/dist/js/bootstrap'
import 'bootstrap/dist/css/bootstrap'
import 'stylesheets/application'
Rails.start()
Turbolinks.start()
ActiveStorage.start()
Snapshot

All the following steps are going to be performed in order to test Boostrap, webpacker and Rails 6.
Inside the app/javascript/stylesheets/application.scss file, add the following background color:
# app/javascript/stylesheets/application.scss
body { background-color: green; }
It'll look something like the following:

In order to stop the Rails Server, let's press the following command:
CTRL + C
rails server
Output

You should be able to see your page.

You should be able to see successful compilation messages.

node:internal/process/promises:279
triggerUncaughtException(err, true /* fromPromise */);
^
Error: Cannot find package '@babel/plugin-proposal-private-methods' imported from /home/calaca/var/www/edume/babel-virtual-resolve-base.js
Snapshot

Solution
yarn add @babel/plugin-proposal-private-methods
npm install @babel/plugin-proposal-private-methods

Error: [BABEL]: --- PLACEHOLDER PACKAGE ---
This @babel/plugin-proposal-private-property-in-object version is not meant to
be imported. Something is importing
@babel/plugin-proposal-private-property-in-object without declaring it in its
dependencies (or devDependencies) in the package.json file.
Add "@babel/plugin-proposal-private-property-in-object" to your devDependencies
to work around this error. This will make this message go away.
(While processing: /home/calaca/var/www/edume/node_modules/@babel/plugin-proposal-private-property-in-object/lib/index.js)
at Object.<anonymous> (/home/calaca/var/www/edume/node_modules/@babel/plugin-proposal-private-property-in-object/lib/index.js:28:7)
at Module._compile (/home/calaca/var/www/edume/node_modules/v8-compile-cache/v8-compile-cache.js:192:30)
at Object.Module._extensions..js (node:internal/modules/cjs/loader:1159:10)
at Module.load (node:internal/modules/cjs/loader:981:32)
at Function.Module._load (node:internal/modules/cjs/loader:822:12)
at Module.require (node:internal/modules/cjs/loader:1005:19)
at require (/home/calaca/var/www/edume/node_modules/v8-compile-cache/v8-compile-cache.js:159:20)
at loadPartialConfigAsync (/home/calaca/var/www/edume/node_modules/@babel/core/lib/config/index.js:34:85)
at Object.<anonymous> (/home/calaca/var/www/edume/node_modules/babel-loader/lib/index.js:126:26)
Snapshot

Solution
npm install --save-dev @babel/plugin-proposal-private-property-in-object
CTRL + C
rails server -p 4000

