turbo-rails: `Turbo is not defined` when trying to call `Turbo.visit`
Hey guys,
I have an app with turbo-rails gem and Im *not using webpack.
On application.html layout I have:
<%= turbo_include_tags %>
<%= stimulus_include_tags %>
but when trying to call Turbo inside stimulus controller (or on browser console) I got the error:
Turbo is not defined
I saw that on response its export { turbo_es2017Esm as Turbo, cable }; but since Im not using webpack I dont think its going to be exported to window object ?
Thanks
About this issue
- Original URL
- State: closed
- Created 3 years ago
- Reactions: 1
- Comments: 16 (3 by maintainers)
@leonardoprg I have an app that I’ve migrated from using Turbolinks to using Turbo. Along the way, I was getting the same error as above (
Turbo is not defined) when I tried to use Turbo functions in Stimulus controllers.During the migration process I did a few find-and-replace commands from ‘Turbolinks’ to ‘Turbo’ and ‘turbolinks’ to ‘turbo’ and such. One of the replaces that I didn’t pay attention to was in the application.js file, where
//= require turbolinksbecame//=require turbo. As far as I can tell, this is what is causing theexport { turbo_es2017Esm as Turbo, cable };error (due to asset concatenation during asset compilation). Consequently, when a Turbo function was then used from Stimulus (or anywhere else for that matter), Turbo wasn’t properly loaded and caused theTurbo is not definederror.A Solution that worked for me
To get Turbo.js to be available in Stimulus and everywhere else, I added
= turbo_include_tagsto my application.html.slim layout. I also removed the spurious//= require turbofrom my application.js fileSide Effects
Though this causes an extra request to the server to fetch the Turbo.js file (since it’s not in the asset pipeline compilation), it was worth it for me to move on from setting up Turbo, to actually using Turbo.
Hope this helps get you or a future reader past this issue.