Skip to content

tsImport() ​

Load TypeScript asynchronously from JavaScript without enabling tsx for unrelated imports. This fits tools that load user configuration or plugins.

Install tsx and check ESM registration compatibility. See scope and lifetime for shared limitations and module formats for execution and export interoperability.

Arguments and return value ​

Import tsImport from tsx/esm/api. tsImport(specifier, options) returns a promise resolving to the imported module's exports.

ArgumentTypeDefaultMeaning
specifierstringRequiredModule specifier to load
optionsstring or options object belowRequiredImporting file context and optional loading settings

A string second argument is the importing file's URL string or absolute path. Pass import.meta.url from ESM or __filename from CommonJS, not a directory or process.cwd(). Relative specifiers resolve from that file.

The object form accepts:

OptionTypeDefaultMeaning
parentURLstringRequiredSame importing file context as the string argument
tsconfigstring | falseNormal lookup, including TSX_TSCONFIG_PATHExisting configuration path for ESM registration, or false to disable its lookup
onImport(url: string) => voidOmitted: no callbackObserve ESM loader activity; see notification semantics

Relative configuration paths use the working directory, not parentURL. The internal CommonJS registration uses its own normal configuration lookup; the tsconfig option does not configure it.

Usage ​

Create this file in a project with tsx installed:

config.mts

ts
export default { port: 3000 } satisfies { port: number }

ESM ​

Create this file beside config.mts:

load.mjs

js
import { tsImport } from 'tsx/esm/api'

const loaded = await tsImport('./config.mts', import.meta.url)
console.log(loaded.default.port)

Run from that project directory:

sh
node ./load.mjs

It prints:

text
3000

The example's default export is available as .default.

CommonJS ​

Create this file beside the same config.mts:

load.cjs

js
const { tsImport } = require('tsx/esm/api')

const main = async () => {
    const loaded = await tsImport('./config.mts', __filename)
    console.log(loaded.default.port)
}

main().catch((error) => {
    console.error(error)
    process.exitCode = 1
})
sh
node ./load.cjs

It prints:

text
3000

Caching and lifetime ​

Each call creates a fresh namespace. Loading the same TypeScript file in a later call evaluates it again instead of reusing the earlier call's namespaced module instance. Imports within one namespace still use module caching.

The scoped hooks remain registered for later import() or require() calls from the loaded code. There is no unregister handle. For a reusable namespace and explicit ESM cleanup, use scoped ESM registration.

tsconfig.json ​

Custom tsconfig.json path ​

Create this file in the example's project directory:

tsconfig.custom.json

json
{}

Replace the call in load.mjs with this fragment:

js
const loaded = await tsImport('./config.mts', {
    parentURL: import.meta.url,
    tsconfig: './tsconfig.custom.json'
})

Run node ./load.mjs; expected output remains:

text
3000

Disable tsconfig.json lookup ​

As an alternative, replace the call in load.mjs with this fragment to disable ESM configuration lookup:

js
const loaded = await tsImport('./config.mts', {
    parentURL: import.meta.url,
    tsconfig: false
})

Run node ./load.mjs; expected output remains:

text
3000

Tracking loaded files ​

Replace the call in load.mjs with this fragment:

js
const loaded = await tsImport('./config.mts', {
    parentURL: import.meta.url,
    onImport: (url) => {
        console.log(url)
    }
})

Run node ./load.mjs. In addition to 3000, the output includes the loaded file's URL, with a project-dependent path. Expected URL excerpt:

text
file:///…/config.mts