Cannot find module '#rails/ujs' - Rails 6 Webpack App - ruby-on-rails

I have these errors in a Rails 6 app and all javascript is not working:
ERROR in ./app/javascript/packs/application.js
Module not found: Error: Can't resolve '#rails/activestorage' in '/Users/jrsahuquillo/Dropbox/myappname/app/javascript/packs'
# ./app/javascript/packs/application.js 9:0-31
ERROR in ./app/javascript/packs/application.js
Module not found: Error: Can't resolve '#rails/ujs' in '/Users/jrsahuquillo/Dropbox/myappname/app/javascript/packs'
# ./app/javascript/packs/application.js 5:0-21
My package.json is:
{
"name": "myappname",
"private": true,
"dependencies": {
"#rails/actioncable": "^6.0.0-alpha",
"#rails/activestorage": "^6.0.2",
"#rails/ujs": "^6.0.2",
"#rails/webpacker": "4.2.0",
"bootstrap": "^4.4.1",
"jquery": "^3.4.1",
"jquery-ui": "^1.12.1",
"jquery-ui-dist": "^1.12.1",
"popper.js": "^1.16.0",
"serialize-javascript": "2.1.2",
"turbolinks": "^5.2.0"
},
"version": "0.1.0",
"devDependencies": {
"webpack-dev-server": "^3.8.0"
},
"optionalDependencies": {
"fsevents": "2.0.0"
}
}
In application.js:
require("#rails/ujs").start();
require("turbolinks").start();
require("#rails/activestorage").start();
require("channels");
require("bootstrap/dist/js/bootstrap");
In app/views/layouts/application.html.erb:
<%= stylesheet_link_tag 'application', media: 'all', 'data-turbolinks-track': 'reload' %>
<%= stylesheet_pack_tag 'application', media: 'all', 'data-turbolinks-track': 'reload' %>
<%= javascript_pack_tag 'application', 'data-turbolinks-track': 'reload' %>
It was working ok, but I don´t know when and why it left working.
Could someone help me?

Sorry, I solved it yet. Previously to having the error, I installed yarn autoclean, and I discovered later that in .yarnclean there was this:
# asset directories
docs
doc
website
images
assets
I removed this, and error disappeared.

Run the commands. the node_modules has a problem i guess.
yarn add rails-ujs turbolinks
npm install rails-ujs turbolinks

Encountered this error when running system tests and managed to fix it by removing node_modules folder and anything that was cached in /tmp folder and then reran yarn install.
Cause of the issue for me was:
Had recently ran yarn autoclean --init yarn autoclean --force and then tried to reverse it since everything was messed up.

Related

Action Text [Rails 7 Error] vanishing field

Trying to isolate an error I'm getting with the basic setup of a new blog site using the below just so I can test Action_Text for the create blog section.
Every-time i create the below- it works great until i change the <%= form.text_area :content %> to <%= form.rich_text_area :content %> then the text area vanishes????
package.json
{
"name": "app",
"private": "true",
"dependencies": {
"#hotwired/stimulus": "^3.1.0",
"#hotwired/turbo-rails": "^7.1.3",
"#popperjs/core": "^2.11.5",
"#rails/actiontext": "^7.0.3-1",
"bootstrap": "^5.2.0",
"bootstrap-icons": "^1.9.1",
"esbuild": "^0.14.54",
"sass": "^1.54.3",
"trix": "^2.0.0-beta.0"
},
"scripts": {
"build": "esbuild app/javascript/*.* --bundle --sourcemap --outdir=app/assets/builds --public-path=assets",
"build:css": "sass ./app/assets/stylesheets/application.bootstrap.scss:./app/assets/builds/application.css --no-source-map --load-path=node_modules"
}
}
rvm gemset create project
rails new project --css bootstrap
rails g scaffold blog title:string content:text
Rails db:migrate
Rails action_text:install
Rails db:migrate
class Blog < ApplicationRecord
validates_presence_of :title
has_rich_text :content
end
<%= form.rich_text_area :content %>
I have searched for days online, tested, broken this multiple times and still cant find a solution.
Any assistance would be great.
Thank you

How do I install a template (CSS & JS) with Webpackers in Rails 6?

I am trying to install this theme - Cleopatra - built on Tailwind CSS in my Rails 6 app, but I can't quite make heads & tails of how to do it properly with webpacker.
I am sure this should be fairly straightforward, but I don't use any JS frameworks so I don't quite get webpacker and JS package managers other than knowing they are similar to Bundler and Gemfiles.
I have Tailwind CSS successfully installed, and I have tried a variety of things and all generate errors or don't render successfully.
So I would rather just a step by step approach to installing it.
I am using Rails 6.0.3.6 and Ruby 3.0.1p64.
Edit 1
I have been trying the following guide and this is the error I am getting:
ERROR in ./app/javascript/stylesheets/application.scss (./node_modules/css-loader/dist/cjs.js??ref--6-1!./node_modules/postcss-loader/src??ref--6-2!./node_modules/sass-loader/dist/cjs.js??ref--6-3!./app/javascript/stylesheets/application.scss)
┃ Module build failed (from ./node_modules/postcss-loader/src/index.js):
┃ ReferenceError: window is not defined
┃ at Object.<anonymous> (/ACA/node_modules/apexcharts/dist/apexcharts.common.js:6:345884)
┃ at Module._compile (/ACA/node_modules/v8-compile-cache/v8-compile-cache.js:192:30)
┃ at Object.Module._extensions..js (internal/modules/cjs/loader.js:1092:10)
┃ at Module.load (internal/modules/cjs/loader.js:928:32)
┃ at Function.Module._load (internal/modules/cjs/loader.js:769:14)
┃ at Module.require (internal/modules/cjs/loader.js:952:19)
┃ at require (/ACA/node_modules/v8-compile-cache/v8-compile-cache.js:159:20)
┃ at Object.<anonymous> (/ACA/postcss.config.js:5:5)
┃ at Module._compile (/ACA/node_modules/v8-compile-cache/v8-compile-cache.js:192:30)
┃ at Object.Module._extensions..js (internal/modules/cjs/loader.js:1092:10)
┃ at Module.load (internal/modules/cjs/loader.js:928:32)
┃ at Function.Module._load (internal/modules/cjs/loader.js:769:14)
┃ at Module.require (internal/modules/cjs/loader.js:952:19)
┃ at require (/ACA/node_modules/v8-compile-cache/v8-compile-cache.js:159:20)
┃ at module.exports (/ACA/node_modules/import-fresh/index.js:28:9)
┃ at loadJs (/ACA/node_modules/cosmiconfig/dist/loaders.js:9:18)
┃ at Explorer.loadFileContent (/ACA/node_modules/cosmiconfig/dist/createExplorer.js:230:12)
┃ at /ACA/node_modules/cosmiconfig/dist/createExplorer.js:252:21
┃ # ./app/javascript/stylesheets/application.scss 2:26-228
┃ # ./app/javascript/packs/application.js
┃
This is my app/javascript/packs/application.js:
const images = require.context('../img', true)
const imagePath = (name) => images(name, true)
import "../packs/cleopatra";
import "../stylesheets/application.scss";
// import "apexcharts";
import ApexCharts from 'apexcharts';
window.ApexCharts = ApexCharts; // return apex chart
This is my postcss.config.js:
module.exports = {
plugins: [
require('tailwindcss'),
require('autoprefixer'),
require('apexcharts'),
require('postcss-import'),
require('postcss-flexbugs-fixes'),
require('postcss-preset-env')({
autoprefixer: {
flexbox: 'no-2009'
},
stage: 3
})
]
}
This is my app/javascript/stylesheets/application.scss:
#import "tailwindcss/base";
#import "tailwindcss/components";
#import "tailwindcss/utilities";
#import "cleopatra";
I have added apexcharts via yarn.
Both of my custom files are app/javascript/stylesheets/cleopatra.css and app/javascript/packs/cleopatra.js.
This is in my application.html.erb:
<%= stylesheet_link_tag 'application', media: 'all', 'data-turbolinks-track': 'reload' %>
<%= stylesheet_pack_tag 'application', media: 'all', 'data-turbolinks-track': 'reload' %>
<%= javascript_pack_tag 'application', 'data-turbolinks-track': 'reload' %>
Edit 2
I am using webpacker 4.3.0, and you can see what packs/cleopatra.js looks like here.
Steps:
cloned cleopatra theme repo locally
git clone https://github.com/moesaid/cleopatra.git moesaid_cleopatra
rails new s_67585007 --no-ri --no-rdoc
rails g controller home index
added root route in config/routes.rb
root 'home#index'
added tailwindcss package with requirements
yarn add tailwindcss#compat postcss#7 autoprefixer#9
postcss.config.js
module.exports = {
plugins: [
require('tailwindcss'),
require('autoprefixer'),
require('postcss-import'),
require('postcss-flexbugs-fixes'),
// require('apexcharts'),
require('postcss-preset-env')({
autoprefixer: {
flexbox: 'no-2009'
},
stage: 3
})
]
}
7.cp moesaid_cleopatra/dist/js/script.js s_67585007/app/javascript/packs/cleopatra.js
copied style, images and javascript file inside public folder
cp moesaid_cleopatra/dist/{css,img} s_67585007/public
app/views/layouts/application.html.erb
<!DOCTYPE html>
<html>
<head>
<title>Test67585007</title>
<%= csrf_meta_tags %>
<%= csp_meta_tag %>
<link rel="stylesheet" href="https://kit-pro.fontawesome.com/releases/v5.12.1/css/pro.min.css">
<link rel="stylesheet" type="text/css" href="css/style.css">
<%= stylesheet_link_tag 'application', media: 'all', 'data-turbolinks-track': 'reload' %>
<%= stylesheet_pack_tag 'application', media: 'all', 'data-turbolinks-track': 'reload' %>
<%= javascript_pack_tag 'application', 'data-turbolinks-track': 'reload' %>
</head>
<body>
<%= yield %>
<%= javascript_pack_tag 'cleopatra', 'data-turbolinks-track': 'reload' %>
</body>
</html>
copied html from moesaid_cleopatra/dist/index.html pasted in app/views/layouts/home/index.html.erb and adjusted with app/views/layouts/application.html.erb changes can be checked here
app/javascript/packs/application.js
import "../stylesheets/application.scss";
import ApexCharts from 'apexcharts';
window.ApexCharts = ApexCharts; // return apex chart
app/javascript/stylesheets/application.scss
#import "tailwindcss/base";
#import "tailwindcss/components";
#import "tailwindcss/utilities";
then start rails server
Note: You may need to install some extra package has during compilation shows package missing clipboard

Bootstrap JS functions not loading in Rails 6/Webpacker

Climbing the learning curve with Webpacker and Rails 6.
I've installed Boostrap 4 using Yarn and Webpacker. When I try play with Bootstrap components in a browser's JS console then I get: TypeError: $.fn.button is undefined.
If I remove the Yarn installation add a Bootstrap CDN link in the head of the HTML (the old way) everything works fine. When I remove the CDN links and revert to using Webpacker I get back to the error above.
Here is my Webpacker setup:
// package.json
{
"name": "depot",
"private": true,
"dependencies": {
"#rails/actioncable": "^6.0.0-alpha",
"#rails/activestorage": "^6.0.0-alpha",
"#rails/ujs": "^6.0.0-alpha",
"#rails/webpacker": "^4.0.7",
"bootstrap": "4.2.1",
"jquery": "^3.4.1",
"popper.js": "^1.15.0",
"turbolinks": "^5.2.0"
},
"version": "0.1.0",
"devDependencies": {
"webpack-dev-server": "^3.8.0"
}
}
// enviornment.js
const { environment } = require("#rails/webpacker");
const webpack = require("webpack");
environment.plugins.append(
"Provide",
new webpack.ProvidePlugin({
$: 'jquery/src/jquery',
jQuery: 'jquery/src/jquery',
Popper: ["popper.js", "default"]
})
);
module.exports = environment;
// application.js
require("#rails/ujs").start();
require("turbolinks").start();
require("#rails/activestorage").start();
require("channels");
require("jquery");
require("bootstrap");
import "./src/application.scss";
# layouts/application.html.haml
!!!
%html
%head
%meta{:content => "text/html; charset=UTF-8", "http-equiv" => "Content-Type"}
%title Depot
= csrf_meta_tags
= csp_meta_tag
= stylesheet_pack_tag 'application', media: 'all', 'data-turbolinks-track': 'reload'
= javascript_pack_tag 'application', 'data-turbolinks-track': 'reload'
Is there something I should be doing to export the Bootstrap functions to the global namespace?
Found a forum where this was discussed: https://gorails.com/forum/how-to-use-bootstrap-with-webpack-rails-discussion
They mentioned it could be caused by having jQuery loading multiple times, with the latest declaration preventing Bootstrap's JS from loading. I couldn't find any evidence of this in my project.
For me the fix ended up being a few lines added to the webpack entry point that exposed jQuery to the browser:
# packs/application.js
import JQuery from 'jquery';
window.$ = window.JQuery = JQuery;
I noticed even I removed require 'jquery' from applications.js, jquery still appears in the compiled application.js, so I removed this from enviroment.js
$: 'jquery/src/jquery',
jQuery: 'jquery/src/jquery',
and add this to application.js
import JQuery from 'jquery';
window.$ = window.JQuery = JQuery;
restart rails server, it works!
If you want to know why see this answer

SyntaxError occurs when reading js.erb file with webpacker

I'm currently developing with Rails5 and React.
And to use React, we use a gem called react-rails, and this gem uses webpacker.
At that time, in React, js.erb file was created to refer to the image precompiled with assets.
The information I looked up on the net had rails-erb-loader, so it should have been readable.
However, SyntaxError occurred.
I will write the details below.
ErrorLog
# ./bin/webpack-dev-server
~~~
Module build failed (from ./node_modules/babel-loader/lib/index.js):
SyntaxError: /home/myproject/app/javascript/components/sample.js.erb: Unexpected token (1:1)
> 1 | <% helpers = ActionController::Base.helpers %>
sample.js.erb
<% helpers = ActionController::Base.helpers %>
export const png_path = "<%= helpers.image_path('sample.png') %>"
package.json
{
"name": "Sample",
"private": true,
"dependencies": {
"#babel/preset-react": "^7.0.0",
"#rails/webpacker": "^4.0.7",
"babel-plugin-transform-react-remove-prop-types": "^0.4.24",
"lodash": "^4.17.14",
"prop-types": "^15.7.2",
"rails-erb-loader": "^5.5.2",
"react": "^16.10.2",
"react-dom": "^16.10.2",
"react_ujs": "^2.6.0",
"reactjs-popup": "^1.4.1"
},
"devDependencies": {
"webpack-dev-server": "^3.8.2"
}
}
How can I solve this error?
We will wait for the answer.
This issue was not resolved. Sorry

Rails_uis throw error when creating a new RailsApp

After creating a new ruby 2.6 / rails6 app and
scaffolding the destroy popup is not working.
Any idea whats wrong.. happens out of sudden..
package.json
{
"name": "untitled1",
"private": true,
"dependencies": {
"#rails/actioncable": "^6.0.0-alpha",
"#rails/activestorage": "^6.0.0-alpha",
"#rails/ujs": "^6.0.0-alpha",
"#rails/webpacker": "^4.0.6",
"turbolinks": "^5.2.0",
},
"version": "0.1.0",
"devDependencies": {
"webpack-dev-server": "^3.5.1"
}
}
console shows this error:
rails-ujs.js:22 Uncaught TypeError: Cannot set property 'Rails' of undefined
at rails-ujs.js:22
at rails-ujs.js:37
at rails-ujs.js:38
at Module.<anonymous> (rails-ujs.js:822)
at Module../node_modules/#rails/ujs/lib/assets/compiled/rails-ujs.js (rails-ujs.js:822)
at __webpack_require__ (bootstrap:19)
at Object../app/javascript/packs/application.js (application.js:6)
at __webpack_require__ (bootstrap:19)
at bootstrap:83
at bootstrap:83
github.com/rails/webpacker/issues/2109,
solves the problem,
comment out //corejs: 3, in babel.config.js
Hopefully the bug will be solved soon.
For anyone that is as catastrophically dumb as I am with this Q/A as my only possible lead (that will take you nowhere) – My issue with Rails being undefined was that my javascript_include_tag was missing. In short, make sure you have:
<!-- app/views/layouts/application.html -->
<!-- turbolinks optional -->
<%= javascript_include_tag 'application', 'data-turbolinks-track': 'reload' %>

Resources