JavaScript Import Attributes (ES2025)

Understanding the new import attributes syntax and why we can't rely on file extensions alone

JavaScript now has a feature that makes module imports more explicit and secure. Import Attributes (part of ES2025) add a way to pass metadata about any module we’re importing, primarily used today for JSON and CSS modules.

The Core Problem

On the web, file extensions don’t reliably indicate content. A server might return JavaScript when you’re expecting JSON:


The Solution: Import Attributes

Import Attributes create a contract between your code and the runtime. You explicitly declare what type of module you expect:

The syntax works consistently across different module contexts:


Why Import Attributes?

The core problem is security. On the web, file extensions don’t reliably indicate content. A URL ending in .json might actually serve JavaScript:

Import Attributes make our module system more explicit and secure. JSON modules can’t execute code - they’re pure data. When you mark a module with type: "json", you’re guaranteed to get either JSON data or an error, never executable code.

The most immediate impact is on JSON imports, where security is crucial:

This feature creates a foundation for safely handling different module types in JavaScript. As part of ES2025, we now have a standard way to declare our expectations about modules across all JavaScript environments. Currently, "json" and "css" are the standardized type values supported by browsers.

Think of it as adding type safety to your module imports - not for the data inside the modules, but for the modules themselves.

Sources


Trevor I. Lasn

Trevor I. Lasn

Building whiteheap.com and 0xinsider.com.



Related Articles

Check out these related articles that might be useful for you. They cover similar topics and provide additional insights.

JavaScript
3 min read

Use the &&= operator to safely update truthy values while preserving falsy states

Nov 5, 2024
Read article
JavaScript
4 min read

How the Suspense component manages async dependencies and improves loading states in Vue apps

Aug 23, 2024
Read article
JavaScript
5 min read

The Stage 2 pipeline operator passes a value through a chain of calls left to right, with % marking where it goes, instead of nesting h(g(f(x))).

Oct 29, 2024
Read article
JavaScript
7 min read

How was JavaScript written back in the day? I dug into early frameworks and libraries to see how code looked then and what still holds up.

Jun 12, 2025
Read article
JavaScript
6 min read

The creator of JavaScript and Node.js are challenging Oracle's control over the JavaScript name

Nov 23, 2024
Read article
JavaScript
19 min read

Common causes of JavaScript memory leaks in Node.js and Deno, such as global objects and callbacks that hold large data, and how to profile heap usage.

Aug 10, 2024
Read article
JavaScript
6 min read

Closures are essential for creating functions that maintain state, without relying on global variables.

Sep 6, 2024
Read article
JavaScript
4 min read

A left shift multiplies a number by 2 per position, and a right shift divides it by 2. How JavaScript's bitwise shift operators work, with binary examples.

Nov 12, 2024
Read article
JavaScript
4 min read

Intermediate variables name each step of complex logic, and early returns handle failing cases first. A JavaScript authentication check, rewritten both ways.

Aug 30, 2024
Read article

This article was originally published on https://www.trevorlasn.com/blog/import-attributes-in-javascript. It was written by a human and polished using grammar tools for clarity.