我在Rails 6应用程序中遇到这些错误,并且所有javascript都不起作用:
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
我的package.json
是:
{
"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"
}
}
在application.js
中:
require("@rails/ujs").start();
require("turbolinks").start();
require("@rails/activestorage").start();
require("channels");
require("bootstrap/dist/js/bootstrap");
在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' %>
它工作正常,但我不知道它何时以及为何退出工作。 有人可以帮我吗?
答案 0 :(得分:2)
运行命令。我想node_modules有问题。
yarn add rails-ujs turbolinks
npm install rails-ujs turbolinks
答案 1 :(得分:0)
对不起,我解决了。在发生错误之前,我安装了yarn autoclean
,后来发现在.yarnclean
中存在此问题:
# asset directories
docs
doc
website
images
assets
我删除了它,错误消失了。