Skip to content

import

In JavaScript, the import keyword brings bindings that another module exports into the current module, and in its import() form loads a module on demand. Static import declarations work only at the top level of a module and pair with export.

A declaration names its bindings in braces, receives the default export without braces, or collects every export into a namespace object with * as. The from clause gives the module specifier, which must be a string literal, and as renames a binding.

Imported bindings are live and read-only. They reflect later changes made by the exporting module, but assigning to one throws a TypeError. Imported modules run before the importing module’s body, wherever the declarations appear.

The dynamic import() syntax, added in ES2020, returns a promise for the module’s namespace object. It works in scripts too, and its specifier can be any expression. Both forms accept import attributes, introduced by with and added in ES2025, which have been Baseline Newly available since 2025.

JavaScript import Keyword Syntax

The import keyword appears in these forms:

Language: JavaScript Syntax
import <defaultName> from "<specifier>";
import { <name> [as <alias>], ... } from "<specifier>";
import * as <namespace> from "<specifier>";
import "<specifier>";
import <defaultName> from "<specifier>" with { type: "json" };

import(<specifier> [, { with: { type: "json" } }])

JavaScript import Keyword Examples

The prices.mjs module exports a constant and a function:

Language: JavaScript Filename: prices.mjs
export const TAX_RATE = 0.2;

export function withTax(price) {
  return price * (1 + TAX_RATE);
}

A second module, checkout.mjs, imports withTax by name and loads node:path on demand:

Language: JavaScript Filename: checkout.mjs
import { withTax } from "./prices.mjs";

console.log(`Total: ${withTax(50)}`);

const path = await import("node:path");
console.log(`Extension: ${path.extname("invoice.pdf")}`);

Running checkout.mjs prints both results:

Language: Shell
$ node checkout.mjs
Total: 60
Extension: .pdf

The dynamic import() call returns a promise, so the code uses await to get the namespace object and reads extname() from it.

Module Information With import.meta

Inside a module, the import.meta meta-property is an object with information about the current module. ECMAScript added it in ES2020 but leaves its properties to the host. Browsers and Node.js both set import.meta.url, and Node.js also sets import.meta.filename and import.meta.dirname, which replace CommonJS’s __filename and __dirname:

Language: JavaScript Filename: info.mjs
import { basename } from "node:path";

console.log(`Scheme: ${new URL(import.meta.url).protocol}`);
console.log(`File: ${basename(import.meta.filename)}`);

Running info.mjs with Node.js prints the URL scheme and the module’s file name:

Language: Shell
$ node info.mjs
Scheme: file:
File: info.mjs

Like static import declarations, import.meta only works in modules.

JavaScript import Keyword Use Cases

  • Using functions, classes, and constants that other modules export
  • Loading Node.js built-in modules with the node: prefix
  • Loading heavy modules only when a feature needs them, with import()
  • Importing JSON data with the type: "json" attribute
  • Finding the current module’s file path with import.meta

Have a question about this? Mentor AI can show you examples, compare related terms, and point you to tutorials.


By Leodanis Pozo Ramos • Updated Nov. 4, 2026