I know Import Maps aren't all that new, however I've recently been messing around with them in a demo app that is natively using ESModules and it's such a nice developer experience!
For those that aren't aware, import maps allow you to configure/tell the browser how it should resolve bare specifiers when it encounters them. Here's an example in practice:
This import map will "remap" any "react"
bare specifier to "https://esm.sh/react@19.0.0"
, which is using esm.sh to transform any npm package to a compatible ESModule on the fly!
Now we can ship code that looks like this to the browser and it will work like it would in Node.js, Deno, Bun, or your favorite JS runtime!
Import Maps are cool and all - but I also wanted to highlight a really cool feature of using them along with package.json#imports
within an isomorphic application.
package.json#imports
(the "imports"
key within a package.json
file) is similar to the import map concept but for Node.js projects. It allows code being run within Node to similarly resolve import specifiers to different locations, here's an example mapping #framework/*
imports to a specific directory:
We can combine these two features to build an isomorphic application, and importantly we can also resolve imports differently based on the environment (e.g. resolve a package that uses Node.js features on the server, and then a stub or alternative on the client)!
I'll leave a bit of this as an exercise to the reader, but I do have an example codebase here that you can look at to see how it works in practice if you don't want to derive the solution yourself 😅.