找不到模块'@ rails / ujs'-Rails 6 Webpack App

时间:2019-12-15 14:01:01

标签: ruby-on-rails rails-activestorage webpacker ruby-on-rails-6 rails-ujs

我在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' %>

它工作正常,但我不知道它何时以及为何退出工作。 有人可以帮我吗?

2 个答案:

答案 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

我删除了它,错误消失了。