[ Web Proxy ]
URL:
Viewing: https://developer.mozilla.org/ja/docs/Web/JavaScript/Reference/Operators/import [Back]  [Original]

import() - JavaScript | MDN

MDN Web Docs

View in English Always switch to English

import()

Baseline

20201

import() ECMAScript

js
import(moduleName)
import(moduleName, options)

import() import const myImport = import SyntaxError

options

moduleName

options

with

  • moduleName
  • moduleName
  • Node Error TypeError
    • Node.js
    • CORS HTTP 404500

: import()

import (import something from "somewhere") import

  • : eval

HTML script type="module" import import

options

js
import("./data.json", { with: { type: "json" } });

import()

2 (import * as name from moduleName)

nullArray.prototype.sort() default [Symbol.toStringTag] "Module" Object.prototype.toString()

Object.getOwnPropertyDescriptors() - -

js
import * as mod from "/my-module.js";

import("/my-module.js").then((mod2) => {
  console.log(mod === mod2); // true
});

1 thenable my-module.js then()

js
// my-module.js
export function then(resolve) {
  console.log("then() called");
  resolve(1);
}
js
// main.js
import * as mod from "/my-module.js";

import("/my-module.js").then((mod2) => {
  // Logs "then() called"
  console.log(mod === mod2); // false
});

: then()

JavaScript HTTP JavaScript URL

js
import(`/my-module.js?t=${Date.now()}`);

API

CSP

3 HTTP fetch()

js
(async () => {
  if (somethingIsTrue) {
    // 
    await import("/modules/my-module.js");
  }
})();

ESM

default default

js
(async () => {
  if (somethingIsTrue) {
    const {
      default: myDefault,
      foo,
      bar,
    } = await import("/modules/my-module.js");
  }
})();

import() await

js
const main = document.querySelector("main");
for (const link of document.querySelectorAll("nav > a")) {
  link.addEventListener("click", (e) => {
    e.preventDefault();

    import("/modules/my-module.js")
      .then((module) => {
        module.loadPageInto(main);
      })
      .catch((err) => {
        main.textContent = err.message;
      });
  });
}

document navigator API

js
let myModule;

if (typeof window === "undefined") {
  myModule = await import("module-used-on-server");
} else {
  myModule = await import("module-used-in-browser");
}

/modules/module-0.js/modules/module-1.js 10 load

js
Promise.all(
  Array.from({ length: 10 }).map(
    (_, index) => import(`/modules/module-${index}.js`),
  ),
).then((modules) => modules.forEach((module) => module.load()));

import()

js
const data = await import("./data.json", {
  with: { type: "json" },
});

ECMAScript 2027 LanguageSpecification
# sec-import-calls


Web Proxy Viewer  |  New URL  |  Original Page