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

Cannot find module '@rails/ujs' - Rails 6 Webpack App

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

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

抱歉,我已经解决了。之前报错,我安装了yarn autoclean,后来发现在.yarnclean里面有这个:

# asset directories
docs
doc
website
images
assets

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

运行 命令。我猜 node_modules 有问题。

yarn add rails-ujs turbolinks
npm install rails-ujs turbolinks

在 运行 系统测试时遇到此错误,并设法通过删除 node_modules 文件夹和缓存在 /tmp 文件夹中的所有内容来修复它,然后重新 运行 yarn install.

我的问题原因是:

最近 运行 yarn autoclean --init yarn autoclean --force 然后试图扭转它,因为一切都搞砸了。