2

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?

Sahu
  • 183
  • 2
  • 9
  • Sorry for the question, but did you run a `yarn install`? Have you tried to remove your `node_modules` and run again `yarn install`? Like that, everything looks good in the setup rails to me. – beauraF Dec 15 '19 at 14:09
  • @beauraF yes, i tried it, but it still is not working. Thanks. Any other idea? – Sahu Dec 15 '19 at 15:17

3 Answers3

7

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.

Sahu
  • 183
  • 2
  • 9
  • I had installed for months, but got the error suddenly. It was this, I would never know. – Juanse Cora Oct 08 '21 at 04:41
  • yeah this also fixed my issue as well. I absent mindly added yarn clean and it caused me hours of headache with @rails/ujs not found – John Dec 04 '21 at 01:56
3

Run the commands. the node_modules has a problem i guess.

yarn add rails-ujs turbolinks
npm install rails-ujs turbolinks
Chrisphine
  • 164
  • 6
0

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.

Reinis
  • 61
  • 1
  • 3