Importmap Node
A Rails gem that bridges Yarn package management with Rails' importmap asset pipeline. Install npm packages via Yarn, automatically vendor the compiled JS entry point into vendor/javascript/, and add a pin to config/importmap.rb — all in one command.
No bundler (webpack, esbuild, etc.) required.
Requirements
- Ruby >= 3.1
- Rails >= 7.0
-
Yarn available on
$PATH - An existing
config/importmap.rb(i.e. your app already usesimportmap-rails)
Installation
Add to your Gemfile:
gem "importmap-node"Then run:
bundle installUsage
Install a package
rails importmap:node:install[lodash]This will:
- Run
yarn add lodash - Find the package's JS entry point from
node_modules/lodash/package.json - Copy it to
vendor/javascript/ - Append a
pin "lodash", to: "lodash.js"line toconfig/importmap.rb - Record the package in
config/importmap_node.jsonfor future updates
Install from a Git repository
You can install packages directly from GitHub or any Git URL:
# Full HTTPS URL
rails importmap:node:install[app-modal@https://github.com/tieeeeen1994/app-modal]Install a local package
rails importmap:node:install[file:../my-lib]The real package name is read from the local package.json and the package is installed as <name>@file:<path>.
Remove a package
rails importmap:node:remove[lodash]This will:
- Run
yarn remove lodash - Delete the vendored file from
vendor/javascript/ - Remove the
pinline fromconfig/importmap.rb - Remove the entry from
config/importmap_node.json
Update all packages
rails importmap:node:updateRe-runs yarn up for all packages tracked in config/importmap_node.json, then re-vendors and re-pins each one.
How it works
The gem maintains a config/importmap_node.json file that records all managed package specs. On each install, only the single JS entry point of a package (resolved via the module → main → index.js fields in package.json) is copied into vendor/javascript/. Transitive dependencies and non-JS assets are not vendored.
config/importmap_node.json — tracks installed package specs
config/importmap.rb — pin lines are added/removed here
vendor/javascript/ — vendored JS entry-point files land here
Programmatic API
installer = Importmap::Node::Installer.new # uses Rails.root
installer = Importmap::Node::Installer.new(root: "/path/to/app")
installer.install("lodash") # install & vendor a package
installer.uninstall("lodash") # remove & unvendor a package
installer.update # update all tracked packages