3.2.1 • Published 10 months ago

xnr v3.2.1

Weekly downloads
-
License
Apache-2.0
Repository
-
Last release
10 months ago

xnr

banner

npm npm type definitions license install size

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

Key Features

  • Zero configuration: Run your TypeScript files directly without needing a tsconfig file 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

Getting Started

Installation

While you can use xnr directly with npx, you can also install it for frequent use:

npm install --save-dev xnr

Running a Script

Simply use npx to run your TypeScript or JavaScript file:

npx xnr file.ts

For running dev scripts in your package.json:

{
  "scripts": {
    "run": "xnr run.ts"
  }
}

Caveats and Scope

  • Only supports dynamic imports or requires with static strings (e.g. require("./file.ts") will work but require(someVar) will not)
  • Requires Node.js 16.14 or higher (for full ES module support)

CLI

CLI docs can be viewed at any time by running xnr --help.

API

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)

Jest transformer

Add these lines to your jest config to get easy TS transforms:

{
  // ...
  "extensionsToTreatAsEsm": [".ts"],
  "transform": {
    "\\.ts$": "<rootDir>/node_modules/xnr/dist/jest.js"
  }
  // ...
}

Key benchmarks

runnerrun single filerun small projectinstall sizeinstall time
xnr93ms102ms0.4MBvery quick
tsx142ms146ms29.7MBslow
swc-node232ms235ms62.0MBvery slow
ts-node661ms659ms6.7MBquick

In general, you can expect best-in-class install + run time.

Contributing

Feel free to open issues if you encounter bugs or have suggestions for new features.

Licence

Apache-2.0

3.0.0-beta.0

10 months ago

3.0.0-beta.2

10 months ago

2.0.0-beta.0

11 months ago

2.0.1

11 months ago

2.0.0

11 months ago

3.0.4

10 months ago

3.2.1

10 months ago

3.0.3

10 months ago

3.2.0

10 months ago

3.0.2

10 months ago

3.1.0

10 months ago

3.0.1

10 months ago

3.0.5

10 months ago

3.0.0

10 months ago

1.1.4

11 months ago

1.1.3

2 years ago

1.1.1

2 years ago

1.1.0

2 years ago

1.1.2

2 years ago

1.0.1

3 years ago

1.0.0

3 years ago

0.4.0-beta.2

3 years ago

0.4.0-beta.1

3 years ago

0.4.0-beta.0

3 years ago

0.3.2

3 years ago

0.3.1

3 years ago

0.3.0-alpha.1

3 years ago

0.3.0-alpha.0

3 years ago

0.2.1

3 years ago

0.2.0

3 years ago

0.1.1

3 years ago

0.1.0

3 years ago

0.0.1-beta.1

3 years ago

0.0.1-alpha.5

3 years ago

0.0.1-alpha.4

3 years ago

0.0.1-alpha.3

3 years ago

0.0.1-alpha.2

3 years ago

0.0.1-alpha.1

3 years ago

0.0.1-alpha

3 years ago