JavaScript Import Attributes (ES2025)

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

Trevor I. Lasn Trevor I. Lasn
· Updated · 3 min read
Building 0xinsider.com — the Bloomberg terminal for prediction markets.

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

Building 0xinsider.com — the Bloomberg terminal for prediction markets. Product engineer based in Tartu, Estonia, building and shipping for over a decade.


Found this article helpful? You might enjoy my free newsletter. I share dev tips and insights to help you grow your coding skills and advance your tech career.


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

navigator.clipboard - The Asynchronous Clipboard API in JavaScript

Copy and paste text, images, and files using the new navigator.clipboard API

Dec 7, 2024
Read article
Javascript
3 min read

Float16Array in JavaScript

Understanding the new 16-bit floating point array in JavaScript

Apr 14, 2025
Read article
Javascript
4 min read

Embrace Intermediate Variables and Early Returns in JavaScript

Early returns and intermediate variables make your code easier to reason about

Aug 30, 2024
Read article
Javascript
4 min read

Intl.DurationFormat: Format Time Durations with Locale Support

Stop writing manual duration formatting code. Instead, leverage the new powerful Intl.DurationFormat API for internationalized time displays

Mar 13, 2025
Read article
Javascript
6 min read

AggregateError in JavaScript

Handle multiple errors at once

Sep 2, 2024
Read article
Javascript
3 min read

JavaScript's &&= Operator: Understanding Logical AND Assignment

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

Nov 5, 2024
Read article
Javascript
5 min read

Precise Decimal Math in JavaScript with Fraction.js

How to handle exact decimal calculations in JavaScript when floating-point precision isn't good enough

Nov 16, 2024
Read article
Javascript
4 min read

Understanding Bitwise Shifts in JavaScript: << and >>

A practical guide to left and right shift operators in JavaScript

Nov 12, 2024
Read article
Javascript
8 min read

JavaScript Sets and Maps: Beyond Arrays and Objects

How to handle unique values and key-value pairs properly without type coercion and performance issues

Nov 17, 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.