英文:
Uncaught Error: Cannot find module '@hotwired/turbo-rails'
问题
I am using the Shakapacker https://github.com/reactjs/react-rails and I have come across this error despite following all the steps in the Get started with shakapacker section I am not sure why this is happening any help is appreciated
config/importmap.rb
# Pin npm packages by running ./bin/importmap
pin "application", preload: true
pin "@hotwired/turbo-rails", to: "turbo.min.js", preload: true
pin "@hotwired/stimulus", to: "stimulus.min.js", preload: true
pin "@hotwired/stimulus-loading", to: "stimulus-loading.js", preload: true
pin_all_from "app/javascript/controllers", under: "controllers"
app/javascript/application.js
import "@hotwired/turbo-rails"
import "controllers"
picture of error
英文:
I am using the Shakapacker https://github.com/reactjs/react-rails and I have come across this error despite following all the steps in the Get started with shakapacker section I am not sure why this is happening any help is appreciated
config/importmap.rb
# Pin npm packages by running ./bin/importmap
pin "application", preload: true
pin "@hotwired/turbo-rails", to: "turbo.min.js", preload: true
pin "@hotwired/stimulus", to: "stimulus.min.js", preload: true
pin "@hotwired/stimulus-loading", to: "stimulus-loading.js", preload: true
pin_all_from "app/javascript/controllers", under: "controllers"
app/javascript/application.js
import "@hotwired/turbo-rails"
import "controllers"
picture of error
答案1
得分: 2
你混淆了概念。Shakapacker
和 Import Maps
是构建前端的两种不同方式。
- Shakapacker:它基于 webpack,因此它运行一个带有编译/转译步骤的 Node.js 打包工具。依赖项在
package.json
中定义。 - Import Maps:基于 ESM,因此它直接在浏览器中运行,没有打包步骤。这是新的 Rails 7+ 默认选项。
首先,你需要选择其中一种选项来构建你的前端代码(你可能可以混合使用它们,但如果你不清楚它们的工作原理,可能会很混乱)。如果你只需要 "Hotwire" 而不想使用任何复杂的功能(如 Tree-shaking、热重载等),importmap-rails
解决方案更简单,也是推荐的方式。
还有第三个选项(我个人偏好的):jsbundling-rails。它是一个小型的层/粘合代码,用于使用 js 打包工具(esbuild、webpack 或 rollup),并依赖于 Rails 资源管道(Sprockets)来插入应用程序中。如果你想看一个示例,一段时间前我发布了一个现成的模板:https://github.com/ralixjs/rails-ralix-tailwind。
英文:
You're mixing concepts. Shakapacker
and Import Maps
are 2 different ways of building your front-end.
- Shakapacker: it's based on webpack, so you are running a Node.js bundler with a compilation/transpilation step. Dependencies are defined in the
package.json
. - Import Maps: based on ESM so it runs directly in the browser with no bundling step. It's the new Rails 7+ default.
So first of all, you'll need to choose one of the options to build your front-end code (you can probably mix both, but it will be messy if you don't have a clear understanding of how they work). If you just need "Hotwire" and don't want any kind of complex stuff (three-shaking, hot-reloading, ...), the importmap-rails
solution is simpler and the recommended way to go.
There is also a 3rd option (my 🙋♂️ preferred one): jsbundling-rails. It's a small layer/glue-code to use a js bundler (esbuild, webpack or rollup) and rely on the Rails assets pipeline (Sprockets) to plug-in into the app. I you want to see an example of this, some time ago I published a ready-to-go template: https://github.com/ralixjs/rails-ralix-tailwind.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论