importmap-rails: Javascript errors after running rails importmap:install on fresh Rails app.

After adding the importmap-rails gem to a freshly generated Rails app’s Gemfile and running rails importmap:install, I’m seeing the following javascript console error messages:

An import map is added after module script load was triggered.

Uncaught TypeError: Failed to resolve module specifier "application". Relative references must start with either "/", "./", or "../".

I have tested this with fresh Rails 6.1 and 7.0a apps in Chrome and Safari.

About this issue

  • Original URL
  • State: closed
  • Created 5 years ago
  • Comments: 22 (19 by maintainers)

Most upvoted comments

After disabling the Apollo Client Devtools Chrome extension, the Stimulus controllers are working and I’m not seeing any console messages (phew!).

Thanks for helping me out, sorry for wasting your time 😃

Ha. Damn extensions. No sweat. It’s a good point we should actually document on importmap-rails. Please do take a swing at a PR for it!

Possible you have some sort of Chrome plugin that’s loading some JS on the page that’s not visible in the HTML you pasted? Because as you can see, the error message does not line up with the HTML. The importmap is the first thing loaded, before any js modules.

@tubsandcans omg, thanks so much for figuring this out. I was going crazy why it suddenly stopped working 😒 God damn extensions!

Ah, I take that you didn’t start this new app with --skip-javascript? If so, you’ll get an app skeleton prepared for Webpacker. That won’t work side by side with import map without some manual intervention. This is a known consideration. But we could document that in a section on how to migrate from Webpacker to import maps. Please do have a look at that!

Import maps have the restriction that if you load any JavaScript module before the import map, then the import map will be blocked.

For example:

<script type="module">
console.log('empty module');
</script>
<script type="importmap">
{ ...importmap }
</script>

will always give this error. It can be fixed by ensuring <script type="importmap"> is never preceded by any <script type="module"> in the HTML of the page.

Additional info that might be helpful:

An import map is added after module script load was triggered. is only happening on Chrome, and I can’t get any Stimulus controllers to connect to the DOM in Chrome. Conversely, in Safari my Stimulus controllers appear to be working.