Movatterモバイル変換


[0]ホーム

URL:


MDN Web Docs

Import attributes

Baseline2025 *
Newly available

Note:A previous version of this proposal used theassert keyword instead ofwith. The assertion feature is now non-standard. Check thebrowser compatibility table for details.

Theimport attributes feature instructs the runtime about how a module should be loaded, including the behavior of module resolution, fetching, parsing, and evaluation. It's supported inimport declarations,export...from declarations, and dynamicimport().

Attributes can be attached to any kind ofimport/export from statement, including default import, namespace import, etc. They follow the module specifier string and start with thewith keyword. When used withimport(), the attributes are specified in theoptions parameter as thewith property.

Syntax

js
import { names } from "module-name" with {};import { names } from "module-name" with { key: "data" };import { names } from "module-name" with { key: "data", key2: "data2" };import { names } from "module-name" with { key: "data", key2: "data2", /* …, */ keyN: "dataN" };export { names } from "module-name" with {};export { names } from "module-name" with { key: "data" };export { names } from "module-name" with { key: "data", key2: "data2" };export { names } from "module-name" with { key: "data", key2: "data2", /* …, */ keyN: "dataN" };

Parameters

keyN

An attribute key. Can be an identifier or a string literal. All keys must be unique and must be known to the runtime.

"dataN"

An attribute value. Must be a string literal.

Description

Import attributes tell the runtime how a particular module should be loaded.

The primary use case is to load non-JS modules, such as JSON modules and CSS modules. Consider the following statement:

js
import data from "https://example.com/data.json";

On the web, each import statement results in a HTTP request. The response is then prepared into a JavaScript value and made available to the program by the runtime. For example, the response may look like this:

http
HTTP/1.1 200 OKContent-Type: application/json; charset=utf-8...{"name":"Maria"}

Modules are identified and parsed only according to their servedMIME type — the file extension in the URL cannot be used to identify a file's type. In this case, the MIME type isapplication/json, which tells the browser that the file is JSON and must be parsed as JSON. If, for some reason (e.g., the server is hijacked or bogus), the MIME type in the server response is set totext/javascript (for JavaScript source), then the file would be parsed and executed as code. If the "JSON" file actually contains malicious code, theimport declaration would unintentionally execute external code, posing a serious security threat.

Import attributes fix this problem by allowing the author to explicitly specify how a module should be validated. For example, the import statement above, which lacks an attribute, would actually fail:

Failed to load module script: Expected a JavaScript module script but the server responded with a MIME type of "application/json". Strict MIME type checking is enforced for module scripts per HTML spec.

Instead, you must provide an attribute to tell the runtime that this file must contain JSON. To validate the module's type (via MIME type), you use the attribute key calledtype. To validate that the module is a JSON module, the value is"json".

Note:The actualtype attribute value does not correspond directly to the MIME type. It's separately specified by theHTML specification.

Therefore, the code above should be re-written as:

js
import data from "https://example.com/data.json" with { type: "json" };

Thetype attribute changes how the module is fetched (the browser sends the request withAccept: application/json header), but doesnot change how the module is parsed or evaluated. The runtime already knows to parse the module as JSON given the response MIME type. It only uses the attribute to doafter-the-fact checking that thedata.json module is, in fact, a JSON module. For example, if the response header changes toContent-Type: text/javascript instead, the program will fail with a similar error as above.

The specification explicitly calls outtype: "json" to be supported — if a module is asserted to betype: "json" and the runtime does not fail this import, then it must be parsed as JSON. However, there's no behavior requirement otherwise: for imports without atype: "json" attribute, the runtime may still parse it as JSON if security is not an issue in this environment. Browsers, on the other hand, implicitly assume that the module is JavaScript, and fail if the module is not JavaScript (for example, JSON). This ensures that module types are always strictly validated and prevents any security risks. In reality, non-browser runtimes such as Node and Deno align with browser semantics and enforcetype for JSON modules.

Thetype attribute also supports other module types. For example, the HTML spec also defines thecss type, which imports aCSSStyleSheet object:

js
import styles from "https://example.com/styles.css" with { type: "css" };

The attributes syntax is designed to be extensible — although onlytype is specified by the language, the runtime can read and process other attributes. An attribute can change the runtime's behavior at every stage of the module loading process:

  • Resolution: the attribute is part of the module specifier (the string in thefrom clause). Therefore, given the same string path, different attributes may lead to entirely different modules being loaded. For example,TypeScript supports theresolution-mode attribute.

    ts
    import type { TypeFromRequire } from "pkg" with { "resolution-mode": "require" };
  • Fetching: for example, CSS modules are fetched with thedestination set to"style", and JSON modules are fetched withdestination: "json". This means given the same destination URL, the server may still return different content.

  • Parsing and evaluation: the runtime may use the attribute to determine how to parse and evaluate the module.

However, you cannot use unknown attributes — the runtime throws an error if it encounters an unknown attribute.

Examples

Importing JSON modules with the type attribute

Indata.json:

json
{  "name": "Shilpa"}

Inindex.html:

html
<!doctype html><html lang="en-US">  <head>    <meta charset="utf-8" />    <script type="module">      import data from "./data.json" with { type: "json" };      const p = document.createElement("p");      p.textContent = `name: ${data.name}`;      document.body.appendChild(p);    </script>  </head>  <body></body></html>

Start a local HTTP server (seetroubleshooting) and go to theindex.html page. You should seeShilpa on the page.

Note:JSON modules only have one default export. You cannot do named imports from them (likeimport { name } from "data.json").

Specifications

Specification
ECMAScript® 2026 Language Specification
# prod-WithClause

Browser compatibility

See also

Help improve MDN

Learn how to contribute.

This page was last modified on byMDN contributors.


[8]ページ先頭

©2009-2025 Movatter.jp