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.
| Argument | Type | Default | Meaning |
|---|---|---|---|
specifier | string | Required | Module specifier to load |
options | string or options object below | Required | Importing 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:
| Option | Type | Default | Meaning |
|---|---|---|---|
parentURL | string | Required | Same importing file context as the string argument |
tsconfig | string | false | Normal lookup, including TSX_TSCONFIG_PATH | Existing configuration path for ESM registration, or false to disable its lookup |
onImport | (url: string) => void | Omitted: no callback | Observe 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
export default { port: 3000 } satisfies { port: number }ESM
Create this file beside config.mts:
load.mjs
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:
node ./load.mjsIt prints:
3000The example's default export is available as .default.
CommonJS
Create this file beside the same config.mts:
load.cjs
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
})node ./load.cjsIt prints:
3000Caching 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
{}Replace the call in load.mjs with this fragment:
const loaded = await tsImport('./config.mts', {
parentURL: import.meta.url,
tsconfig: './tsconfig.custom.json'
})Run node ./load.mjs; expected output remains:
3000Disable tsconfig.json lookup
As an alternative, replace the call in load.mjs with this fragment to disable ESM configuration lookup:
const loaded = await tsImport('./config.mts', {
parentURL: import.meta.url,
tsconfig: false
})Run node ./load.mjs; expected output remains:
3000Tracking loaded files
Replace the call in load.mjs with this fragment:
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:
file:///…/config.mts