import
BaselineWidely available *
This feature is well established and works across many devices and browser versions. It’s been available across browsers since May 2018.
* Some parts of this feature may have varying levels of support.
The staticimport
declaration is used to import read-only livebindings which areexported by another module. The imported bindings are calledlive bindings because they are updated by the module that exported the binding, but cannot be re-assigned by the importing module.
In order to use theimport
declaration in a source file, the file must be interpreted by the runtime as amodule. In HTML, this is done by addingtype="module"
to the<script>
tag. Modules are automatically interpreted instrict mode.
There is also a function-like dynamicimport()
, which does not require scripts oftype="module"
.
Syntax
import defaultExport from "module-name";import * as name from "module-name";import { export1 } from "module-name";import { export1 as alias1 } from "module-name";import { default as alias } from "module-name";import { export1, export2 } from "module-name";import { export1, export2 as alias2, /* … */ } from "module-name";import { "string name" as alias } from "module-name";import defaultExport, { export1, /* … */ } from "module-name";import defaultExport, * as name from "module-name";import "module-name";
defaultExport
Name that will refer to the default export from the module. Must be a valid JavaScript identifier.
module-name
The module to import from. Only single quoted and double quoted string literals are allowed. The evaluation of the specifier is host-specified. Most hosts align with browsers and resolve the specifiers as URLs relative to the current module URL (see
import.meta.url
). Node, bundlers, and other non-browser environments often define their own features on top of this, so you should find documentation for them to understand the exact rules. Themodule specifier resolution section also has more information.name
Name of the module object that will be used as a kind of namespace when referring to the imports. Must be a valid JavaScript identifier.
exportN
Name of the exports to be imported. The name can be either an identifier or a string literal, depending on what
module-name
declares to export. If it is a string literal, it must be aliased to a valid identifier.aliasN
Names that will refer to the named imports. Must be a valid JavaScript identifier.
The"module-name"
may be followed by a set ofimport attributes, starting with thewith
keyword.
Description
import
declarations can only be present in modules, and only at the top-level (i.e., not inside blocks, functions, etc.). If animport
declaration is encountered in non-module contexts (for example,<script>
tags withouttype="module"
,eval
,new Function
, which all have "script" or "function body" as parsing goals), aSyntaxError
is thrown. To load modules in non-module contexts, use thedynamic import syntax instead.
All imported bindings cannot be in the same scope as any other declaration, includinglet
,const
,class
,function
,var
, andimport
declaration.
import
declarations are designed to be syntactically rigid (for example, only string literal specifiers, only permitted at the top-level, all bindings must be identifiers), which allows modules to be statically analyzed and linked before getting evaluated. This is the key to making modules asynchronous by nature, powering features liketop-level await.
Forms of import declarations
There are four forms ofimport
declarations:
- Named import:
import { export1, export2 } from "module-name";
- Default import:
import defaultExport from "module-name";
- Namespace import:
import * as name from "module-name";
- Side effect import:
import "module-name";
Below are examples to clarify the syntax.
Named import
Given a value namedmyExport
which has been exported from the modulemy-module
either implicitly asexport * from "another.js"
or explicitly using theexport
statement, this insertsmyExport
into the current scope.
import { myExport } from "/modules/my-module.js";
You can import multiple names from the same module.
import { foo, bar } from "/modules/my-module.js";
You can rename an export when importing it. For example, this insertsshortName
into the current scope.
import { reallyReallyLongModuleExportName as shortName } from "/modules/my-module.js";
A module may also export a member as a string literal which is not a valid identifier, in which case you must alias it in order to use it in the current module.
// /modules/my-module.jsconst a = 1;export { a as "a-b" };
import { "a-b" as a } from "/modules/my-module.js";
Note:import { x, y } from "mod"
is not equivalent toimport defaultExport from "mod"
and then destructuringx
andy
fromdefaultExport
. Named and default imports are distinct syntaxes in JavaScript modules.
Default import
Default exports need to be imported with the corresponding default import syntax. This version directly imports the default:
import myDefault from "/modules/my-module.js";
Since the default export doesn't explicitly specify a name, you can give the identifier any name you like.
It is also possible to specify a default import with namespace imports or named imports. In such cases, the default import will have to be declared first. For instance:
import myDefault, * as myModule from "/modules/my-module.js";// myModule.default and myDefault point to the same binding
or
import myDefault, { foo, bar } from "/modules/my-module.js";
Importing a name calleddefault
has the same effect as a default import. It is necessary to alias the name becausedefault
is a reserved word.
import { default as myDefault } from "/modules/my-module.js";
Namespace import
The following code insertsmyModule
into the current scope, containing all the exports from the module located at/modules/my-module.js
.
import * as myModule from "/modules/my-module.js";
Here,myModule
represents anamespace object which contains all exports as properties. For example, if the module imported above includes an exportdoAllTheAmazingThings()
, you would call it like this:
myModule.doAllTheAmazingThings();
myModule
is asealed object withnull
prototype. The default export is available as a key calleddefault
. For more information, seemodule namespace object.
Note:JavaScript does not have wildcard imports likeimport * from "module-name"
, because of the high possibility of name conflicts.
Import a module for its side effects only
Import an entire module for side effects only, without importing anything. This runsthe module's global code, but doesn't actually import any values.
import "/modules/my-module.js";
This is often used forpolyfills, which mutate the global variables.
Hoisting
Import declarations arehoisted. In this case, that means that the identifiers the imports introduce are available in the entire module scope, and their side effects are produced before the rest of the module's code runs.
myModule.doAllTheAmazingThings(); // myModule.doAllTheAmazingThings is imported by the next lineimport * as myModule from "/modules/my-module.js";
Module specifier resolution
The ECMAScript specification does not define how module specifiers are resolved and leaves it to the host environment (e.g., browsers, Node.js, Deno). Browser behavior is specified bythe HTML spec, and this has become thede facto baseline for all environments.
There are three types of specifiers widely recognized, as implemented by the HTML spec, Node, and many others:
- Relative specifiers that start with
/
,./
, or../
, which are resolved relative to the current module URL. - Absolute specifiers that are parsable URLs, which are resolved as-is.
- Bare specifiers that are not one of the above.
The most notable caveat for relative specifiers, especially for people familiar with theCommonJS conventions, is that browsers forbid one specifier to implicitly resolve to many potential candidates. In CommonJS, if you havemain.js
andutils/index.js
, then all of the following will import the "default export" fromutils/index.js
:
// main.jsconst utils = require("./utils"); // Omit the "index.js" file nameconst utils = require("./utils/index"); // Omit only the ".js" extensionconst utils = require("./utils/index.js"); // The most explicit form
On the web, this is costly because if you writeimport x from "./utils"
, the browser needs to send requests toutils
,utils/index.js
,utils.js
, and potentially many other URLs until it finds an importable module. Therefore, in the HTML spec, the specifier by default can only be a URL resolved relative to the current module URL. You cannot omit the file extension or theindex.js
file name. This behavior has been inherited by Node's ESM implementation, but it is not a part of the ECMAScript specification.
Note that this does not mean thatimport x from "./utils"
never works on the web. The browser still sends a request to that URL, and if the server can respond with the correct content, the import will succeed. This requires the server to implement some custom resolution logic, because usually extension-less requests are understood as requests for HTML files.
Absolute specifiers can be any kind ofURL that resolve to importable source code. Most notably:
HTTP URLs are always supported on the web since most scripts already have HTTP URLs. It's supported natively by Deno (which initially predicated its entire module system on HTTP URLs), but it only has experimental support in Node viacustom HTTPS loaders.
file:
URLs are supported by many non-browser runtimes such as Node, since scripts there already havefile:
URLs, but they are not supported by browsers due to security reasons.Data URLs are supported by many runtimes including browsers, Node, Deno, etc. They are useful for embedding small modules directly into the source code. SupportedMIME types are those that designate importable source code, such as
text/javascript
for JavaScript,application/json
for JSON modules,application/wasm
for WebAssembly modules, etc. (They may still requireimport attributes.)js// HTTP URLsimport x from "https://example.com/x.js";// Data URLsimport x from "data:text/javascript,export default 42;";// Data URLs for JSON modulesimport x from 'data:application/json,{"foo":42}' with { type: "json" };
text/javascript
data URLs are still interpreted as modules, but they cannot use relative imports — because thedata:
URL scheme is not hierarchical. That is,import x from "data:text/javascript,import y from './y.js';"
will throw an error because the relative specifier'./y.js'
cannot be resolved.node:
URLs resolve to built-in Node.js modules. They are supported by Node and other runtimes that claim compatibility with Node, such as Bun.
Bare specifiers, popularized by CommonJS, are resolved within thenode_modules
directory. For example, if you haveimport x from "foo"
, then the runtime will look for thefoo
package within anynode_modules
directory in the parent directories of the current module. This behavior can be reproduced in browsers usingimport maps, which also enable you to customize resolution in other ways.
The module resolution algorithm can also be executed programmatically using theimport.meta.resolve
function defined by the HTML spec.
Examples
Standard Import
In this example, we create a re-usable module that exports a function to get all primes within a given range.
// getPrimes.js/** * Returns a list of prime numbers that are smaller than `max`. */export function getPrimes(max) { const isPrime = Array.from({ length: max }, () => true); isPrime[0] = isPrime[1] = false; isPrime[2] = true; for (let i = 2; i * i < max; i++) { if (isPrime[i]) { for (let j = i ** 2; j < max; j += i) { isPrime[j] = false; } } } return [...isPrime.entries()] .filter(([, isPrime]) => isPrime) .map(([number]) => number);}
import { getPrimes } from "/modules/getPrimes.js";console.log(getPrimes(10)); // [2, 3, 5, 7]
Imported values can only be modified by the exporter
The identifier being imported is alive binding, because the module exporting it may re-assign it and the imported value would change. However, the module importing it cannot re-assign it. Still, any module holding an exported object can mutate the object, and the mutated value can be observed by all other modules importing the same value.
You can also observe the new value through themodule namespace object.
// my-module.jsexport let myValue = 1;setTimeout(() => { myValue = 2;}, 500);
// main.jsimport { myValue } from "/modules/my-module.js";import * as myModule from "/modules/my-module.js";console.log(myValue); // 1console.log(myModule.myValue); // 1setTimeout(() => { console.log(myValue); // 2; my-module has updated its value console.log(myModule.myValue); // 2 myValue = 3; // TypeError: Assignment to constant variable. // The importing module can only read the value but can't re-assign it.}, 1000);
Specifications
Specification |
---|
ECMAScript® 2026 Language Specification # sec-imports |
Browser compatibility
See also
export
import()
import.meta
- Import attributes
- Previewing ES6 Modules and more from ES2015, ES2016 and beyond on blogs.windows.com (2016)
- ES6 in Depth: Modules on hacks.mozilla.org (2015)
- ES modules: A cartoon deep-dive on hacks.mozilla.org (2018)
- Exploring JS, Ch.16: Modules by Dr. Axel Rauschmayer
- Export and Import on javascript.info