ARCHIVED
This package was developed in a time when:
- node did not have native support for stripping TS types
- tsx was immature
- deno had no npm support
- bun was not stable
I am going to reuse the npm:xnr package for a different project starting at v4.
Tool xnr file.tsbecomesDocs Node.js (native) node file.tsRunning TypeScript Natively, API: Modules TypeScript tsx npx tsx file.tstsx.dev, npm: tsx Deno deno run file.tsTypeScript support, deno runBun bun run file.tsBun TypeScript, bun runFlag-free
node file.tsrequires one of these minimum versions, per major:
Node major Min version for native node file.ts18, 20 never available - use tsx(or Deno/Bun)22 22.18.0 23 23.6.0 24+ 24.0.0 (stable since 24.12.0)
- On any of the versions above: use native type stripping -
node file.ts, no flag, no install.- On any older Node: use
npx tsx file.ts. tsx needs Node >= 18 and supports the full TypeScript language, including JSX.- On Node < 18: tsx is unsupported; upgrade Node or use Deno/Bun, which both run TypeScript natively with no flags.
Note: native type stripping only erases types - no enums, no namespaces with runtime code, no parameter properties, no JSX and no type checking (
tsc --noEmitremains your job). If you need those, tsx is the closest drop-in replacement for xnr.
Easily, quickly, and reliably run a TypeScript Node.js script from the CLI with zero configuration. Blazingly fast 🚀
npx xnr any-file.{ts,tsx,cts,mts,js,jsx,cjs,mjs}Ideal for utility scripts, quick debugging and CI pipelines
- Zero configuration: Run your TypeScript files directly without needing a
tsconfigfile or any additional setup. Ideal for quick scripts or CI tasks - Supports multiple file types: Easily run any combination of TypeScript, JavaScript, JSON and JSX files
- Lightweight: Very quick to install at just ~400kB
- Flexible and familiar: Tolerant to different file extensions, but otherwise follows Node conventions
- Optimised for Speed: Faster install+execution time than
xnr - Focused Scope: Single goal: run TypeScript code quickly and reliably
- Supports Windows
While you can use xnr directly with npx, you can also install it for frequent use:
npm install --save-dev xnrSimply use npx to run your TypeScript or JavaScript file:
npx xnr file.tsFor running dev scripts in your package.json:
{
"scripts": {
"run": "xnr run.ts"
}
}- Only supports dynamic imports or requires with static strings (e.g.
require("./file.ts")will work butrequire(someVar)will not) - Requires Node.js 16.14 or higher (for full ES module support)
CLI docs can be viewed at any time by running xnr --help.
xnr also provides an API with a few more options than the CLI:
// Runs a file with auto-transpilation of it and its dependencies, as required.
const run: (filePathOrConfig: string | RunConfig) => Promise<number>;
// Converts all local source code starting from an entry file into a directly runnable directory of Node.js compatible code.
const build: ({
filePath,
outputDirectory,
}: {
filePath: string;
outputDirectory: string;
}) => Promise<Output>;
// Converts all local source code starting from an entry file into a runnable array of Node.js compatible file contents.
const transpile: ({ filePath }: { filePath: string }) => Promise<Output>;
// Transforms an input code string into a Node-friendly ECMAScript Module (ESM) code string. Unlike the others here, it doesn't rewrite imports.
const transform: ({ code, filePath }: { code: string; filePath?: string }) => Promise<string>;A complete list of exports can be viewed on
npmjs.com (navigate to
/xnr/dist/lib/index.d.ts)
Add these lines to your jest config to get easy TS transforms:
{
// ...
"extensionsToTreatAsEsm": [".ts"],
"transform": {
"\\.ts$": "<rootDir>/node_modules/xnr/dist/jest.js"
}
// ...
}| runner | run single file | run small project | install size | install time |
|---|---|---|---|---|
| xnr | 93ms |
102ms |
0.4MB |
very quick |
| tsx | 142ms |
146ms |
29.7MB |
slow |
| swc-node | 232ms |
235ms |
62.0MB |
very slow |
| ts-node | 661ms |
659ms |
6.7MB |
quick |
In general, you can expect best-in-class install + run time.
Feel free to open issues if you encounter bugs or have suggestions for new features.
Apache-2.0