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)

Most upvoted comments

@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 turbolinks became //=require turbo. As far as I can tell, this is what is causing the export { 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 the Turbo is not defined error.

A Solution that worked for me

To get Turbo.js to be available in Stimulus and everywhere else, I added = turbo_include_tags to my application.html.slim layout. I also removed the spurious //= require turbo from my application.js file

Side 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.