importmap-rails: Popper.js not working when --download is used

I apologize in advance if this is not a importmap problem, I am still learning to use it.

I have problem with bootstrap configuration through importmaps, for some reason when I run bin/importmap pin bootstrap --download I am getting these errors in chrome console (jsdelivr, unpkg) cdn providers

GET http://localhost:3000/assets/modifiers/index.js net::ERR_ABORTED 404 (Not Found)
@popperjs--core-ce872054c46039b8d5939756969a7a008e8d1ab3907e72b141e9e4650641b9ea.js:1 GET http://localhost:3000/assets/enums.js net::ERR_ABORTED 404 (Not Found)
@popperjs--core-ce872054c46039b8d5939756969a7a008e8d1ab3907e72b141e9e4650641b9ea.js:6 GET http://localhost:3000/assets/popper.js net::ERR_ABORTED 404 (Not Found)
@popperjs--core-ce872054c46039b8d5939756969a7a008e8d1ab3907e72b141e9e4650641b9ea.js:8 GET http://localhost:3000/assets/popper-lite.js net::ERR_ABORTED 404 (Not Found)
@popperjs--core-ce872054c46039b8d5939756969a7a008e8d1ab3907e72b141e9e4650641b9ea.js:4 GET http://localhost:3000/assets/createPopper.js net::ERR_ABORTED 404 (Not Found)

when I use command without download option like this bin/importmap pin bootstrap it works properly…not sure what is the problem in both commands it uses same cdn link

errors below are from JSPM cdn provider

GET http://localhost:3000/_/40866a73.js net::ERR_ABORTED 404 (Not Found)
@popperjs--core-139c8b1356b983f829b396b480ea4fa4e5205f8045a1c02bccb5024bda4fded6.js:1 GET http://localhost:3000/assets/enums.js net::ERR_ABORTED 404 (Not Found)
@popperjs--core-139c8b1356b983f829b396b480ea4fa4e5205f8045a1c02bccb5024bda4fded6.js:1 GET http://localhost:3000/assets/modifiers/index.js net::ERR_ABORTED 404 (Not Found)
@popperjs--core-139c8b1356b983f829b396b480ea4fa4e5205f8045a1c02bccb5024bda4fded6.js:1 GET http://localhost:3000/assets/modifiers/eventListeners.js net::ERR_ABORTED 404 (Not Found)
@popperjs--core-139c8b1356b983f829b396b480ea4fa4e5205f8045a1c02bccb5024bda4fded6.js:1 GET http://localhost:3000/assets/modifiers/popperOffsets.js net::ERR_ABORTED 404 (Not Found)
@popperjs--core-139c8b1356b983f829b396b480ea4fa4e5205f8045a1c02bccb5024bda4fded6.js:1 GET http://localhost:3000/assets/modifiers/offset.js net::ERR_ABORTED 404 (Not Found)
@popperjs--core-139c8b1356b983f829b396b480ea4fa4e5205f8045a1c02bccb5024bda4fded6.js:1 GET http://localhost:3000/assets/modifiers/applyStyles.js net::ERR_ABORTED 404 (Not Found)
@popperjs--core-139c8b1356b983f829b396b480ea4fa4e5205f8045a1c02bccb5024bda4fded6.js:1 GET http://localhost:3000/assets/modifiers/flip.js net::ERR_ABORTED 404 (Not Found)
@popperjs--core-139c8b1356b983f829b396b480ea4fa4e5205f8045a1c02bccb5024bda4fded6.js:1 GET http://localhost:3000/assets/modifiers/preventOverflow.js net::ERR_ABORTED 404 (Not Found)
@popperjs--core-139c8b1356b983f829b396b480ea4fa4e5205f8045a1c02bccb5024bda4fded6.js:1 GET http://localhost:3000/assets/modifiers/arrow.js net::ERR_ABORTED 404 (Not Found)
@popperjs--core-139c8b1356b983f829b396b480ea4fa4e5205f8045a1c02bccb5024bda4fded6.js:1 GET http://localhost:3000/assets/modifiers/hide.js net::ERR_ABORTED 404 (Not Found)
@popperjs--core-139c8b1356b983f829b396b480ea4fa4e5205f8045a1c02bccb5024bda4fded6.js:1 GET http://localhost:3000/assets/popper-lite.js net::ERR_ABORTED 404 (Not Found)
@popperjs--core-139c8b1356b983f829b396b480ea4fa4e5205f8045a1c02bccb5024bda4fded6.js:1 GET http://localhost:3000/assets/utils/detectOverflow.js net::ERR_ABORTED 404 (Not Found)
@popperjs--core-139c8b1356b983f829b396b480ea4fa4e5205f8045a1c02bccb5024bda4fded6.js:1 GET http://localhost:3000/assets/dom-utils/getBoundingClientRect.js net::ERR_ABORTED 404 (Not Found)
@popperjs--core-139c8b1356b983f829b396b480ea4fa4e5205f8045a1c02bccb5024bda4fded6.js:1 GET http://localhost:3000/assets/dom-utils/getCompositeRect.js net::ERR_ABORTED 404 (Not Found)
@popperjs--core-139c8b1356b983f829b396b480ea4fa4e5205f8045a1c02bccb5024bda4fded6.js:1 GET http://localhost:3000/assets/dom-utils/getNodeScroll.js net::ERR_ABORTED 404 (Not Found)
@popperjs--core-139c8b1356b983f829b396b480ea4fa4e5205f8045a1c02bccb5024bda4fded6.js:1 GET http://localhost:3000/assets/dom-utils/getWindowScroll.js net::ERR_ABORTED 404 (Not Found)
@popperjs--core-139c8b1356b983f829b396b480ea4fa4e5205f8045a1c02bccb5024bda4fded6.js:1 GET http://localhost:3000/assets/dom-utils/getWindow.js net::ERR_ABORTED 404 (Not Found)
@popperjs--core-139c8b1356b983f829b396b480ea4fa4e5205f8045a1c02bccb5024bda4fded6.js:1 GET http://localhost:3000/assets/dom-utils/instanceOf.js net::ERR_ABORTED 404 (Not Found)
@popperjs--core-139c8b1356b983f829b396b480ea4fa4e5205f8045a1c02bccb5024bda4fded6.js:1 GET http://localhost:3000/assets/dom-utils/getHTMLElementScroll.js net::ERR_ABORTED 404 (Not Found)
@popperjs--core-139c8b1356b983f829b396b480ea4fa4e5205f8045a1c02bccb5024bda4fded6.js:1 GET http://localhost:3000/assets/dom-utils/getNodeName.js net::ERR_ABORTED 404 (Not Found)
@popperjs--core-139c8b1356b983f829b396b480ea4fa4e5205f8045a1c02bccb5024bda4fded6.js:1 GET http://localhost:3000/assets/dom-utils/getWindowScrollBarX.js net::ERR_ABORTED 404 (Not Found)
@popperjs--core-139c8b1356b983f829b396b480ea4fa4e5205f8045a1c02bccb5024bda4fded6.js:1 GET http://localhost:3000/assets/dom-utils/getDocumentElement.js net::ERR_ABORTED 404 (Not Found)
@popperjs--core-139c8b1356b983f829b396b480ea4fa4e5205f8045a1c02bccb5024bda4fded6.js:1 GET http://localhost:3000/assets/dom-utils/isScrollParent.js net::ERR_ABORTED 404 (Not Found)
@popperjs--core-139c8b1356b983f829b396b480ea4fa4e5205f8045a1c02bccb5024bda4fded6.js:1 GET http://localhost:3000/assets/dom-utils/getComputedStyle.js net::ERR_ABORTED 404 (Not Found)
@popperjs--core-139c8b1356b983f829b396b480ea4fa4e5205f8045a1c02bccb5024bda4fded6.js:1 GET http://localhost:3000/assets/dom-utils/getLayoutRect.js net::ERR_ABORTED 404 (Not Found)
@popperjs--core-139c8b1356b983f829b396b480ea4fa4e5205f8045a1c02bccb5024bda4fded6.js:1 GET http://localhost:3000/assets/dom-utils/listScrollParents.js net::ERR_ABORTED 404 (Not Found)
@popperjs--core-139c8b1356b983f829b396b480ea4fa4e5205f8045a1c02bccb5024bda4fded6.js:1 GET http://localhost:3000/assets/dom-utils/getParentNode.js net::ERR_ABORTED 404 (Not Found)
@popperjs--core-139c8b1356b983f829b396b480ea4fa4e5205f8045a1c02bccb5024bda4fded6.js:1 GET http://localhost:3000/assets/dom-utils/getOffsetParent.js net::ERR_ABORTED 404 (Not Found)
@popperjs--core-139c8b1356b983f829b396b480ea4fa4e5205f8045a1c02bccb5024bda4fded6.js:1 GET http://localhost:3000/_/08d18af5.js net::ERR_ABORTED 404 (Not Found)
@popperjs--core-139c8b1356b983f829b396b480ea4fa4e5205f8045a1c02bccb5024bda4fded6.js:1 GET http://localhost:3000/_/03a0b0f1.js net::ERR_ABORTED 404 (Not Found)
@popperjs--core-139c8b1356b983f829b396b480ea4fa4e5205f8045a1c02bccb5024bda4fded6.js:1 GET http://localhost:3000/_/5328de10.js net::ERR_ABORTED 404 (Not Found)
@popperjs--core-139c8b1356b983f829b396b480ea4fa4e5205f8045a1c02bccb5024bda4fded6.js:1 GET http://localhost:3000/assets/utils/getMainAxisFromPlacement.js net::ERR_ABORTED 404 (Not Found)
@popperjs--core-139c8b1356b983f829b396b480ea4fa4e5205f8045a1c02bccb5024bda4fded6.js:1 GET http://localhost:3000/_/8937ec25.js net::ERR_ABORTED 404 (Not Found)
@popperjs--core-139c8b1356b983f829b396b480ea4fa4e5205f8045a1c02bccb5024bda4fded6.js:1 GET http://localhost:3000/assets/utils/getOppositePlacement.js net::ERR_ABORTED 404 (Not Found)
@popperjs--core-139c8b1356b983f829b396b480ea4fa4e5205f8045a1c02bccb5024bda4fded6.js:1 GET http://localhost:3000/assets/utils/computeAutoPlacement.js net::ERR_ABORTED 404 (Not Found)
@popperjs--core-139c8b1356b983f829b396b480ea4fa4e5205f8045a1c02bccb5024bda4fded6.js:1 GET http://localhost:3000/assets/utils/getOppositeVariationPlacement.js net::ERR_ABORTED 404 (Not Found)
@popperjs--core-139c8b1356b983f829b396b480ea4fa4e5205f8045a1c02bccb5024bda4fded6.js:1 GET http://localhost:3000/_/ea19fa71.js net::ERR_ABORTED 404 (Not Found)
@popperjs--core-139c8b1356b983f829b396b480ea4fa4e5205f8045a1c02bccb5024bda4fded6.js:1 GET http://localhost:3000/_/8d8f2f4c.js net::ERR_ABORTED 404 (Not Found)
@popperjs--core-139c8b1356b983f829b396b480ea4fa4e5205f8045a1c02bccb5024bda4fded6.js:1 GET http://localhost:3000/_/c591086d.js net::ERR_ABORTED 404 (Not Found)
@popperjs--core-139c8b1356b983f829b396b480ea4fa4e5205f8045a1c02bccb5024bda4fded6.js:1 GET http://localhost:3000/_/6e98d37c.js net::ERR_ABORTED 404 (Not Found)
@popperjs--core-139c8b1356b983f829b396b480ea4fa4e5205f8045a1c02bccb5024bda4fded6.js:1 GET http://localhost:3000/assets/modifiers/computeStyles.js net::ERR_ABORTED 404 (Not Found)
@popperjs--core-139c8b1356b983f829b396b480ea4fa4e5205f8045a1c02bccb5024bda4fded6.js:1 GET http://localhost:3000/assets/dom-utils/getDocumentRect.js net::ERR_ABORTED 404 (Not Found)
@popperjs--core-139c8b1356b983f829b396b480ea4fa4e5205f8045a1c02bccb5024bda4fded6.js:1 GET http://localhost:3000/assets/dom-utils/getViewportRect.js 404 (Not Found)

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"

import "@popperjs/core"
import "bootstrap"

config/importmap.rb

# Pin npm packages by running ./bin/importmap

pin "application", preload: true
pin "@hotwired/turbo-rails", to: "turbo.js"
pin "@hotwired/stimulus", to: "stimulus.js"
pin "@hotwired/stimulus-loading", to: "stimulus-loading.js"
pin_all_from "app/javascript/controllers", under: "controllers"
pin "bootstrap" # @5.1.3
pin "@popperjs/core", to: "@popperjs--core.js" # @2.10.2

About this issue

  • Original URL
  • State: closed
  • Created 5 years ago
  • Reactions: 11
  • Comments: 30 (12 by maintainers)

Commits related to this issue

Most upvoted comments

Also that doesn’t make any sense, i’m literally using version control and a cdn. “CDN or NPM” is a wild statement to put in their README. We should be applying upstream pressure on maintainers of DOM manipulating libraries to distribute javascript for web browsers.

Updated 2024 version with importmap-rails >2

  • Pull the actual esm from jsdelivr
  • Rename file because of jsdelivr’s obnoxious url route/convention
  • Come back to this thread in 2028
bin/importmap pin @popperjs/core@2.11.8/+esm  --from jsdelivr
mv vendor/javascript/@popperjs--core--+esm.js vendor/javascript/stupid-popper-lib-2024.js
# config/importmap.rb
pin "@popperjs/core", to: "stupid-popper-lib-2024.js"

Note as a quick fix you can also define it - <script>window.process = { env: {} }</script>

My solution for this problem is: Going to bootstrap doc, download the file directly on CDN then, put it into vendor/ directory like this.

vendor/
     bootstrap.min.js
     popper.min.js

In the importmap.rb code just select the right name:

# Added libraries
pin "bootstrap", to: "bootstrap.min.js" 
pin "@popperjs/core", to: "popper.min.js"

Then import them normally, in application.js.

import "bootstrap"
import "@popperjs/core"

Would be nice perhaps with an FAQ about how to resolve particular packages that don’t just work out of the box. If anyone wants to explore that, please open a PR for it.

Please use a versioning management system like a CDN or npm/Yarn."

no

It’s been a year and half now.

Just to be clear: The official Rails Way for dealing with multi-file dependencies is to manually download them and copy them into the project?! (Same solution was also proposed in jspm/generator#153)

So no dependency management, version control, or way to easily keep dependencies updated?

Also, according to the official Popper docs:

Managing dependencies by “directly downloading” them and placing them into your source code is not recommended for a variety of reasons, including missing out on feat/fix updates easily. Please use a versioning management system like a CDN or npm/Yarn.

There needs to be a better way.

Wow, thanks @vietqhoang!

This now works perfectly for me with latest:

1. Pin from JSDelivr

./bin/importmap pin @popperjs/core@latest/+esm --download --from jsdelivr

2. Add the fix for process.env

<!-- In app/views/layouts/application.html.erb -->
<script>
  window.process = { env: { NODE_ENV: 'production'} }
</script>

3. [Optional] Rename

Because I want audit and outdated to work, I manually changed the pin name:

# From this:
pin "@popperjs/core/+esm", to: "@popperjs--core--+esm.js" # @2.11.8

# To this:
pin "@popperjs/core", to: "@popperjs--core--+esm.js" # @2.11.8

That’s all!

✅ One-line install (with some minor manual file tweaking) ✅ No manually copying of vendor files ✅ Correctly bundles all dependencies in a single file ✅ audit and outdated commands work correctly ✅ Easily updatable (just run the command again)

It would be even better if we could specify the pin name as an argument to bin/importmap pin. That way we could eliminate Step 3 entirely.

if (process.env.NODE_ENV !== "production") …uggh that’s an annoying branch to begin with, regardless of esm. Is that common for dist packages to do?

Can confirm modifying my importmap.rb to explicit /esm/index.js resource works. Sort of a bummer since this is a common hello world setup

@rromanchuk @AnalyzePlatypus @lingceng Thanks for the great discussion and the hints (its a shame that one needs to put so much effort in just to donwnload a file and get it running).

In any case, I am using Flowbite (via https://github.com/iwdt/flowbite-rails) and try to pin popper

# bin/importmap pin @popperjs/core@2.11.8/+esm --from jsdelivr
# mv vendor/javascript/@popperjs--core--+esm.js vendor/javascript/popper-lib.js
pin '@popperjs/core', to: 'popper-lib.js' # @2.11.8

and import it accordinglz in my application.js import "@popperjs/core";

For whatever magic reason, everytime I reload my page, my browser makes a request to the URI /npm/@popperjs/core@2.11.8/+esm … which obviously fails. I don’t know where this request is coming from, I grepped all files for popper, but no match.

Some someone have any idea or encountered a similar error?

Need change

pin "@popperjs/core", to: "@popperjs--core--+esm.js" 

to

pin "@popperjs/core", to: "@popperjs--core--esm.js"

And manually change the file name in vender.

Seems ‘+’ is not legal in url path.

In the meantime, here’s the exact workaround I used, for future readers.

This PR from the Symfony team found that JsDelivr’s ESM mode was much better at packaging dependencies than JSPM. Symfony has accordingly migrated their ImportMap implementation to use JsDelivr instead.

However, when I tried running importmap pin with the --from jsdelivr option, it again downloaded an incomplete file without bundled dependencies.

The solution I landed on was:

  1. Run ./bin/importmap pin @popperjs/core --from jsdelivr --download. This downloads the partial Popper file into vendor/javascript/@popperjs--core.js
  2. Visit the JSDelivr page for @popper/core to get the link to the ESM build. (In my case it was https://cdn.jsdelivr.net/npm/@popperjs/core@2.11.8/+esm)
  3. I opened the link in my browser and copied the contents.
  4. I pasted the JS code into vendor/javascript/@popperjs--core.js

Finally, I ran into the process.env issue, so had to apply @guybedford’s fix to supply the process.env variable:

<!-- In app/views/layouts/application.html.erb -->
<script>
  window.process = { env: { NODE_ENV: 'production'} }
</script>

That did the trick for now.

If importmap-rails could generate the correct JsDelivr URL, it seems like this issue could be permanently solved.

I also encounter 404 not found as described in the OP when using ./bin/importmap pin swiper --download. Three files are added to vendor/javascript/:

pin "swiper" # @7.4.1
pin "dom7" # @4.0.4
pin "ssr-window" # @4.0.2

And three files are downloaded to vendor/javascript/: swiper.js, dom7.js and ssr-window.js.

However swiper.js contains this code:

export{A as A11y,d as Autoplay,C as Controller,k as EffectCards,i as EffectCoverflow,j as EffectCreative,g as EffectCube,E as EffectFade,h as EffectFlip,f as FreeMode,G as Grid,c as HashNavigation,H as History,K as Keyboard,L as Lazy,e as Manipulation,M as Mousewheel,N as Navigation,P as Pagination,b as Parallax,a as Scrollbar,S as Swiper,T as Thumbs,V as Virtual,Z as Zoom,S as default}from"./_/6811b57d.js";import"ssr-window";import"dom7";

So it wants to import JS from a file ./_/6811b57d.js, but no such file got downloaded to vendor/javascript, so when rendering the page Error: 404 Not Found http://localhost:3000/assets/_/6811b57d.js is thrown in the JS console.

A workaround is to either avoid the --download option, or to add pin "_/6811b57d" to importmap.rb (beside the three other pins mentioned above) and manually download the file from CDN to vendor/javascript/_/6811b57d.js.