npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

xnr

v3.2.1

Published

Easily, quickly and reliably run a Node.js script from the CLI.

Downloads

2,188

Readme

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

| 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.

Contributing

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

Licence

Apache-2.0