Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails 7 Import Maps - import folder with custom scripts

playing around with Rails 7 and import maps. Trying to import a folder with some custom JS.

# config/importmap.rb
pin_all_from "app/javascript/custom", under: "custom"

# app/javascript/application.js
import "custom"

Gives Uncaught TypeError: Failed to resolve module specifier "custom" on Chrome and Uncaught Error: Unable to resolve specifier 'custom' from [shim] on Firefox

interestingly import "custom/script" works fine.

what am I doing wrong?

like image 819
Phil-6 Avatar asked Jul 01 '26 13:07

Phil-6


1 Answers

I had this same problem and found the following solution:

# app/javascript/application.js
import "custom/my_custom_code.js"

This means that pin_all_from "app/javascript/custom", under: "custom" is really just making each custom JS file available under the custom namespace. Therefore, you still need to import each file individually.

like image 129
Tristan Hart Avatar answered Jul 04 '26 02:07

Tristan Hart



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!