import.defer()
The import.defer() syntax behaves like regular import() syntax, except that it results in a deferred module namespace object. The module and its dependencies are fetched and linked up front, but their synchronous evaluation is deferred until the namespace's properties are accessed.
The **`import.defer()`** syntax behaves like regular [`import()`](/docs/Web/JavaScript/Reference/Operators/import) syntax, except that it results in a [deferred module namespace object](/docs/Web/JavaScript/Reference/Statements/import/defer#deferred_module_namespace_object). The module and its dependencies are [fetched](/docs/Web/JavaScript/Guide/Modules/Module_graph#loading_the_graph) and [linked](/docs/Web/JavaScript/Guide/Modules/Module_graph#linking_modules) up front, but their synchronous [evaluation](/docs/Web/JavaScript/Guide/Modules/Module_graph#evaluating_modules) is deferred until the namespace's properties are accessed.
For more information about deferred evaluation, including its interaction with top-level `await`, see the [`import defer`](/docs/Web/JavaScript/Reference/Statements/import/defer) declaration form.
## Syntax
```js-nolint
import.defer(moduleName)
import.defer(moduleName, options)
```
`import.defer()` is special syntax (a "meta property"), not a method on an `import` object.
### Parameters
See [`import()`](/docs/Web/JavaScript/Reference/Operators/import#parameters).
### Return value
Returns a promise that fulfills with a [deferred module namespace object](/docs/Web/JavaScript/Reference/Statements/import/defer#deferred_module_namespace_object) after the module graph is [loaded](/docs/Web/JavaScript/Guide/Modules/Module_graph#loading_the_graph) and linked, and any eagerly evaluated [top-level `await` dependencies](/docs/Web/JavaScript/Reference/Statements/import/defer#top-level_await) have finished evaluating.
Like regular [`import()`](/docs/Web/JavaScript/Reference/Operators/import#return_value), the promise rejects if the module or its dependencies cannot be loaded, parsed, or linked. It also rejects if an eagerly evaluated module throws. Errors from evaluation that remains deferred are instead thrown synchronously by the namespace operation that triggers evaluation.
## Examples
### Using import.defer()
> [!NOTE]
> It's guaranteed that awaiting the resulting promise never accidentally calls an exported `then` method—a gotcha associated with the [regular module namespace object](/docs/Web/JavaScript/Reference/Operators/import#module_namespace_object)—because the deferred module namespace object never exposes a property called `then`.
```js
const ts = await import.defer("typescript");
function compilePath(path) {
// Evaluation of the typescript module subgraph starts here
const program = ts.createProgram([path], {});
}
```
Never immediately destructure the returned namespace, because it triggers evaluation:
```js example-bad
const { createProgram } = await import.defer("typescript");
// The typescript module has now been evaluated.
```
## Specifications
## Browser compatibility
## See also
- [JavaScript modules](/docs/Web/JavaScript/Guide/Modules) guide
- import()
- import defer
Cite this
"import.defer()". MDN Web Docs. CC BY-SA 2.5 (MDN). https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/import/defer