JavaScript import maps are now supported cross-browser

Celebration This web feature is now available in all three browser engines!
ES modules are a modern way to include and reuse JavaScript code in web applications. They are
supported by modern browsers and provide several benefits over older, non-modul…


This content originally appeared on web.dev and was authored by Thomas Steiner

ES modules are a modern way to include and reuse JavaScript code in web applications. They are supported by modern browsers and provide several benefits over older, non-modular approaches to JavaScript development.

A modern way to use ES modules is with the <script type="importmap"> tag. This tag allows you to define a mapping of external module names to their corresponding URLs, which makes it easier to include and use external modules in your code.

To use the <script type="importmap"> approach, you first need to add it to the <head> section of your HTML document. Inside the tag, you can define a JSON object that maps module names to their corresponding URLs. For example:

<script type="importmap">
{
"imports": {
"lodash": "https://unpkg.com/lodash@4.17.21/lodash.js"
}
}
</script>

This code defines a single external module named "lodash" and maps it to the URL of the lodash library on the unpkg CDN. With this mapping in place, you can now use the import keyword to include the lodash library in your code. Note that the import keyword is only available inside a script tag with the type="module" attribute.

<script type="module">
import _ from 'lodash';

console.log(_.map([1, 2, 3], (n) => n * 2));
</script>

Using the <script type="importmap"> tag and the import keyword provides several benefits over older, non-modular approaches to JavaScript development. It allows you to clearly and explicitly specify the external modules your code depends on, which makes it easier to understand and maintain your code. Overall, using ES modules with the <script type="importmap"> tag is a modern and powerful way to include and reuse JavaScript code in web applications. You can feature-detect support as follows:

if (HTMLScriptElement.supports('importmap')) {
// The importmap feature is supported.
}
Browser support
  • chrome 89, Supported 89
  • firefox 108, Supported 108
  • edge 89, Supported 89
  • safari, Not supported ×

Further reading #

Acknowledgements #

Hero image by CHUTTERSNAP on Unsplash.


This content originally appeared on web.dev and was authored by Thomas Steiner


Print Share Comment Cite Upload Translate Updates
APA

Thomas Steiner | Sciencx (2023-03-28T00:00:00+00:00) JavaScript import maps are now supported cross-browser. Retrieved from https://www.scien.cx/2023/03/28/javascript-import-maps-are-now-supported-cross-browser/

MLA
" » JavaScript import maps are now supported cross-browser." Thomas Steiner | Sciencx - Tuesday March 28, 2023, https://www.scien.cx/2023/03/28/javascript-import-maps-are-now-supported-cross-browser/
HARVARD
Thomas Steiner | Sciencx Tuesday March 28, 2023 » JavaScript import maps are now supported cross-browser., viewed ,<https://www.scien.cx/2023/03/28/javascript-import-maps-are-now-supported-cross-browser/>
VANCOUVER
Thomas Steiner | Sciencx - » JavaScript import maps are now supported cross-browser. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2023/03/28/javascript-import-maps-are-now-supported-cross-browser/
CHICAGO
" » JavaScript import maps are now supported cross-browser." Thomas Steiner | Sciencx - Accessed . https://www.scien.cx/2023/03/28/javascript-import-maps-are-now-supported-cross-browser/
IEEE
" » JavaScript import maps are now supported cross-browser." Thomas Steiner | Sciencx [Online]. Available: https://www.scien.cx/2023/03/28/javascript-import-maps-are-now-supported-cross-browser/. [Accessed: ]
rf:citation
» JavaScript import maps are now supported cross-browser | Thomas Steiner | Sciencx | https://www.scien.cx/2023/03/28/javascript-import-maps-are-now-supported-cross-browser/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.