问题
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?
回答1:
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.
回答2:
Run the commands. the node_modules has a problem i guess.
yarn add rails-ujs turbolinks
npm install rails-ujs turbolinks
来源:https://stackoverflow.com/questions/59344632/cannot-find-module-rails-ujs-rails-6-webpack-app