Skip to content

optionalImport

optionalImport<T>(load): Promise<T | null>

Defined in: packages/node/src/module/optional-import.ts:20

Import a module optionally, returning null if the module is not found.

This is useful for optional peer dependencies, where a module may or may not be installed.

load must contain a literal import("specifier") expression written in the caller’s own module: dynamic import specifiers resolve against the file the expression is written in, not the file that happens to invoke load at runtime. Passing a specifier string here instead would resolve it against this module’s own dependencies, which fails under strict (non-hoisting) package managers whenever the optional dependency belongs to the caller instead.

T

() => Promise<T>

A thunk containing the import("specifier") expression.

Promise<T | null>

The imported module, or null if the module cannot be found.