importmap-rails: TypeError: Module specifier, 'application' does not start with "/", "./", or "../".

Getting this with a fresh Rails 7.0.2.3 app with import maps installed in macOS Safari Version 15.1 (17612.2.9.1.20).

Screen Shot 2022-03-19 at 11 13 42 PM

For context the exported <script> for import maps:

<script type="importmap" data-turbo-track="reload">{
      "imports": {
        "application": "/assets/application-37f365cbecf1fa2810a8303f4b6571676fa1f9c56c248528bc14ddb857531b95.js",
        "@hotwired/turbo-rails": "/assets/turbo.min-96cbf52c71021ba210235aaeec4720012d2c1df7d2dab3770cfa49eea3bb09da.js",
        "@hotwired/stimulus": "/assets/stimulus.min-900648768bd96f3faeba359cf33c1bd01ca424ca4d2d05f36a5d8345112ae93c.js",
        "@hotwired/stimulus-loading": "/assets/stimulus-loading-1fc59770fb1654500044afd3f5f6d7d00800e5be36746d55b94a2963a7a228aa.js",
        "controllers/application": "/assets/controllers/application-368d98631bccbf2349e0d4f8269afb3fe9625118341966de054759d96ea86c7e.js",
        "controllers/hello_controller": "/assets/controllers/hello_controller-549135e8e7c683a538c3d6d517339ba470fcfb79d62f738a0a089ba41851a554.js",
        "controllers": "/assets/controllers/index-2db729dddcc5b979110e98de4b6720f83f91a123172e87281d5a58410fc43806.js"
      }
    }</script>

Hard to diagnose since it doesn’t include any stack traces. Seems potentially related to https://github.com/guybedford/es-module-shims/issues/244 but I wasn’t sure whether to escalate quite yet.

Gemfile

gem "importmap-rails"

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"

$ bin/importmap json:

$ bin/importmap json
{
  "imports": {
    "application": "/assets/application-37f365cbecf1fa2810a8303f4b6571676fa1f9c56c248528bc14ddb857531b95.js",
    "@hotwired/turbo-rails": "/assets/turbo.min-96cbf52c71021ba210235aaeec4720012d2c1df7d2dab3770cfa49eea3bb09da.js",
    "@hotwired/stimulus": "/assets/stimulus.min-900648768bd96f3faeba359cf33c1bd01ca424ca4d2d05f36a5d8345112ae93c.js",
    "@hotwired/stimulus-loading": "/assets/stimulus-loading-1fc59770fb1654500044afd3f5f6d7d00800e5be36746d55b94a2963a7a228aa.js",
    "controllers/application": "/assets/controllers/application-368d98631bccbf2349e0d4f8269afb3fe9625118341966de054759d96ea86c7e.js",
    "controllers/hello_controller": "/assets/controllers/hello_controller-549135e8e7c683a538c3d6d517339ba470fcfb79d62f738a0a089ba41851a554.js",
    "controllers": "/assets/controllers/index-2db729dddcc5b979110e98de4b6720f83f91a123172e87281d5a58410fc43806.js"
  }
}

app/views/layouts/application.html.erb:

<!DOCTYPE html>
<html>
  <head>
    <title>Example</title>
    <meta name="viewport" content="width=device-width,initial-scale=1">
    <%= csrf_meta_tags %>
    <%= csp_meta_tag %>

    <%= stylesheet_link_tag "application", "data-turbo-track": "reload" %>
    <%= javascript_importmap_tags %>
  </head>

  <body>
    <main>
      <%= yield %>
    </main>
  </body>
</html>

app/javascript/application.js:

// Configure your import map in config/importmap.rb. Read more: https://github.com/rails/importmap-rails
import "@hotwired/turbo-rails"
import "controllers"

About this issue

  • Original URL
  • State: closed
  • Created 5 years ago
  • Reactions: 4
  • Comments: 15 (1 by maintainers)

Most upvoted comments

why is this issue closed?

This issue is still persistent. Anyone have a working solution that they can share? Commenting out import “controllers”; is not an option.

The offending import is import "controllers". Commenting it out also fixes the problem. Maybe the root problem in my case (creating a project from scratch following the tutorial) is poorly configured controllers javascript package for the default empty project?

Hi @skd. If removing import "controllers" is a temporary fix, what would be a permanent fix? I’m dealing with the same problem.

The offending import is import "controllers". Commenting it out also fixes the problem. Maybe the root problem in my case (creating a project from scratch following the tutorial) is poorly configured controllers javascript package for the default empty project?

Inserting es-modules-shims before javascript_importmap_tags as mentioned in the importmaps-rails readme resolved this issue for me for legacy browsers

<script async src="https://ga.jspm.io/npm:es-module-shims@1.8.2/dist/es-module-shims.js" data-turbo-track="reload"></script>
<%= javascript_importmap_tags %>

See “Supporting legacy browsers such as Safari on iOS 15” here: https://github.com/rails/importmap-rails?tab=readme-ov-file#supporting-legacy-browsers-such-as-safari-on-ios-15

i have the same issue but with

i only set up my app server with bin/rails server -b 0.0.0.0

TypeError: Module specifier, 'application' does not start with "/", "./", or "../". Referenced from http://192.168.0.122:3000/ es-module-shims.js:34

Had an issue with this recently.

Everything had been fine for 8 months then all of a sudden I had customer complaints.

The issue was that in addition to the es-module-shim I had also added another script tag above javascript_importmap_tags.

As soon as I had moved any script tag that wasn’t es-module-shims below javascript_importmap_tags the issue was resolved.

It seems the order must be

  1. es-module-shim
  2. javascript_importmap_tags
  3. any other script tag

Even with the es-module-shims, my JS still won’t load

IIRC I solved this by renaming app/assets/javascripts/application.js to app/assets/javascripts/application-old.js and updating the include:

<%= javascript_include_tag 'application-old' %>

Not the best solution, but the plan is to over time migrate the code in application-old.js elsewhere.