Skip to content


tsx.require() is an enhanced require() function that can load TypeScript and ESM files.

Use this function for importing TypeScript files in CommonJS mode without adding TypeScript support to the entire runtime.

Note, the current file path must be passed in as the second argument to resolve the import context.


  • import() calls in the loaded files are not enhanced.
  • Because it compiles ESM syntax to run in CommonJS mode, top-level await is not supported

CommonJS usage

const tsx = require('tsx/cjs/api')

const tsLoaded = tsx.require('./file.ts', __filename)
const tsFilepath = tsx.require.resolve('./file.ts', __filename)

ESM usage

import { require } from 'tsx/cjs/api'

const tsLoaded = require('./file.ts', import.meta.url)
const tsFilepath = require.resolve('./file.ts', import.meta.url)

Tracking loaded files

Because the CommonJS API tracks loaded modules in require.cache, you can use it to identify loaded files for dependency tracking. This can be useful when implementing a watcher.

const resolvedPath = tsx.require.resolve('./file', import.meta.url)

const collectDependencies = module => [

// ['/file.ts', '/foo.ts', '/bar.ts']