# importmap-rails
> importmap-rails is the Rails gem that maps module names to files in config/importmap.rb, so the browser loads your JavaScript as native ES modules with no bundler or Node.js build step.

importmap-rails is the Rails gem that lets the browser load your JavaScript as native ES modules, with no bundler and no transpiler. Node.js is not part of the build. You declare each module and package in `config/importmap.rb`, and Rails renders those declarations as a `<script type="importmap">` tag that maps names like `"@hotwired/stimulus"` to digested files served by the asset pipeline. When your code says `import { Controller } from "@hotwired/stimulus"`, the browser reads the map and fetches the right file itself.

## Where it fits in Rails

importmap-rails has been the default JavaScript option since Rails 7.0, and it still is in Rails 8. Running `rails new myapp` without a `--javascript` flag adds the gem, runs its installer, and sets up Turbo and Stimulus on top of it. In Rails 8 it pairs with [Propshaft](https://avohq.io/glossary/propshaft), the asset pipeline that fingerprints and serves the files the import map points at.

The latest release is importmap-rails 2.2.3, published in January 2026 (checked in November 2026). The 2.x line started in January 2024 and changed two defaults that older tutorials get wrong. Both come up below.

## Installing it in an existing app

New apps already have it. For an app that started on Sprockets, Webpacker or jsbundling-rails, add the gem and run the installer.

```ruby
# Gemfile
gem "importmap-rails"
```

```sh
bundle install
bin/rails importmap:install
```

The installer creates `config/importmap.rb`, an empty `app/javascript/application.js` entry point, the `bin/importmap` command, and a `vendor/javascript` directory for downloaded packages. It also adds `<%= javascript_importmap_tags %>` to `app/views/layouts/application.html.erb` when that file exists. If your layout lives elsewhere, add the tag yourself.

## Writing the map with `pin` and `pin_all_from`

`config/importmap.rb` is plain Ruby. Each `pin` maps one name to one file, and `pin_all_from` maps a whole directory under a prefix.

```ruby
# config/importmap.rb
pin "application"
pin "@hotwired/turbo-rails", to: "turbo.min.js"
pin "@hotwired/stimulus", to: "stimulus.min.js"
pin "@hotwired/stimulus-loading", to: "stimulus-loading.js"
pin "chartkick", preload: false

pin_all_from "app/javascript/controllers", under: "controllers"
pin_all_from "app/javascript/src", under: "src"
```

With the last line in place, `import { formatMoney } from "src/money"` resolves to `app/javascript/src/money.js`. Propshaft has no fallback for unpinned local files, so every file you import needs a pin. A missing pin shows up as a module resolution error in the browser console, not as a Rails error. A pin whose file is missing is skipped with a warning in the Rails log.

Since 2.0, importmap-rails preloads every pin by default. The layout emits a `<link rel="modulepreload">` for each one, so the browser fetches the whole graph in parallel instead of discovering imports one level at a time. Add `preload: false` to anything you only load on a few pages, like the chart library above.

Rails reloads `config/importmap.rb` in development when it changes. Removing a pin is the exception. Restart the server to get a removed pin out of the rendered map.

## Adding npm packages with `bin/importmap pin`

```sh
bin/importmap pin local-time
bin/importmap pin lodash-es --from jsdelivr
bin/importmap unpin local-time
```

The command asks the JSPM.org API to resolve the package and its dependencies, then downloads each file into `vendor/javascript` and writes a pin with the version as a comment.

```ruby
pin "local-time" # @3.0.3
```

Since importmap-rails 2.0, pinning always downloads. The 1.x series linked to a CDN URL by default and offered a `--download` flag to vendor the file instead. That flag no longer exists in 2.x because downloading is the only mode. Commit `vendor/javascript` to source control, and your app serves every package from its own domain with no runtime dependency on a CDN. The default resolver is `jspm`; `--from unpkg` and `--from jsdelivr` pick a different source for the download.

The package has to ship an ES module build. Most modern libraries do. A package that only publishes CommonJS, or expects a bundler to resolve Node built-ins, will not work through an import map without a separate build step.

## Loading it in the layout

```erb
<!-- app/views/layouts/application.html.erb -->
<head>
  <%= csrf_meta_tags %>
  <%= stylesheet_link_tag "application", "data-turbo-track": "reload" %>
  <%= javascript_importmap_tags %>
  <%= yield :head %>
</head>
```

`javascript_importmap_tags` renders the JSON map, the modulepreload links, and `<script type="module">import "application"</script>`. To load a module on one page only, pin it with `preload: false` and call `javascript_import_module_tag` from that view. It must render after `javascript_importmap_tags`, which is why the `yield :head` sits below it.

```erb
<% content_for :head do %>
  <%= javascript_import_module_tag "checkout" %>
<% end %>
```

## Stimulus controllers through the map

stimulus-rails detects importmap-rails and registers controllers through the map. The `pin_all_from "app/javascript/controllers", under: "controllers"` line pins every controller file, and `app/javascript/controllers/index.js` registers them by name.

```js
// app/javascript/controllers/index.js
import { application } from "controllers/application"
import { eagerLoadControllersFrom } from "@hotwired/stimulus-loading"
eagerLoadControllersFrom("controllers", application)
```

A new controller needs no extra registration. Create the file, and the pin and the loader pick it up.

```js
// app/javascript/controllers/clipboard_controller.js
import { Controller } from "@hotwired/stimulus"

export default class extends Controller {
  static targets = ["source"]

  copy() {
    navigator.clipboard.writeText(this.sourceTarget.value)
  }
}
```

```erb
<div data-controller="clipboard">
  <input data-clipboard-target="source" value="<%= @invite.url %>" readonly>
  <button data-action="clipboard#copy">Copy link</button>
</div>
```

Run `bin/rails generate stimulus clipboard` if you prefer the generator to write the file.

## Keeping packages current

```sh
bin/importmap json       # print the map exactly as the browser receives it
bin/importmap packages   # list pinned packages with their versions
bin/importmap outdated   # compare pinned versions with the npm registry
bin/importmap audit      # check pinned versions against npm security advisories
bin/importmap update     # re-pin and re-download every outdated package
bin/importmap pristine   # re-download all pins, for example after a bad checkout
```

`bin/importmap json` is the fastest way to debug a module that will not load. If a name is missing from the output, the pin is missing or the server needs a restart. `outdated` and `audit` exit with a non-zero status when they find something, so both fit in a CI step.

For apps that use `stale?` or `fresh_when`, add `stale_when_importmap_changes` to `ApplicationController`. Otherwise a deploy that only changes JavaScript can serve a cached 304 page that points at old files.

## Browser support

Import maps work natively in Chrome and Edge 89, Firefox 108, and Safari 16.4 on macOS and iOS. That covers every current evergreen browser. importmap-rails 2.0 removed the bundled `es-module-shims` polyfill for that reason, and you do not need it for current browsers. If you must support Safari 15 or older iPhones, add the `es-module-shims` script tag before `javascript_importmap_tags`, as the gem's README shows.

## What it does not do

importmap-rails maps names to files. It does not change those files.

- No transpilation. TypeScript, JSX, Vue single-file components and Svelte need a compiler, and import maps have none.
- No bundling or tree shaking. The browser downloads each module as written, including exports you never call.
- No CSS handling. Stylesheets stay with Propshaft, tailwindcss-rails or cssbundling-rails.
- No npm install. There is no `node_modules` and no `package.json` unless you add them for other tools.

## When not to use it

Pick [jsbundling-rails](https://avohq.io/glossary/jsbundling-rails) when you need TypeScript or JSX but want Rails to stay in charge of the asset pipeline, with esbuild, Bun or Rollup writing a build into `app/assets/builds`. Pick [vite_rails](https://avohq.io/glossary/vite-rails) when the front end is a large React, Vue or Svelte app and the team wants hot module replacement and the Vite plugin ecosystem. If your JavaScript is Hotwire plus a few ESM packages, importmap-rails is the smaller setup and the one Rails maintains as its default.

## Summary

importmap-rails turns `config/importmap.rb` into a browser-native import map, so a Rails app can ship ES modules with no build step. Since 2.0 it downloads pinned packages into `vendor/javascript` and preloads every pin by default. It stops at mapping, so code that needs compiling belongs with jsbundling-rails or vite_rails. Avo, the [Rails admin framework](https://avohq.io/rails-admin) that publishes this glossary, ships its own JavaScript, so apps on importmap-rails run it without any build step.
