Socket
Socket
Sign inDemoInstall

tsx

Package Overview
Dependencies
Maintainers
1
Versions
106
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

tsx

Node.js runtime enhanced with esbuild for loading TypeScript & ESM


Version published
Weekly downloads
0
Maintainers
1
Weekly downloads
 
Created

Package description

What is tsx?

The tsx npm package is a CLI tool for running TypeScript and TSX files directly without pre-compilation. It's essentially a TypeScript execution and REPL for node.js, similar to how `node` works for JavaScript. It allows developers to quickly run scripts and prototypes written in TypeScript, including support for TSX (TypeScript with JSX syntax), which is commonly used in React development.

What are tsx's main functionalities?

Execute TypeScript files directly

Allows you to run a TypeScript file directly from the command line without needing to compile it first. This is particularly useful for quick scripts or during development.

tsx script.ts

REPL mode

Launches a REPL (Read-Eval-Print Loop) where you can type and execute TypeScript code interactively. This is useful for testing small snippets of code quickly.

tsx

Support for TSX

Enables execution of TSX files, which is TypeScript with JSX syntax. This is particularly useful for React developers who want to test components or scripts that include JSX.

tsx component.tsx

Other packages similar to tsx

Readme

Source

tsx

Node.js enhanced with esbuild to run TypeScript & ESM

Features

  • Blazing fast on-demand TypeScript & ESM compilation
  • Works in both CommonJS and ESM packages
  • Supports next-gen TypeScript extensions (.cts & .mts)
  • Supports node: import prefixes
  • Hides experimental feature warnings
  • TypeScript REPL
  • Tested on Linux & Windows with Node.js v12~18

Install

npm install --save-dev tsx

Install globally

Install it globally to use it anywhere, outside of your npm project, without npx.

npm install --global tsx

Usage

Note: Commands are prefixed with npx to execute the tsx binary, but it's not necessary if globally installed or when using it in the script object in package.json

Run TypeScript / ESM / CJS module

Pass in a file to run:

npx tsx ./file.ts

Watch mode

Run file and automatically re-run on changes.

All imported files are watched except from the following directories: node_modules, bower_components, vendor, dist, and .* (hidden directories).

Press Return to manually re-run.

npx tsx watch ./file.ts

REPL

Start a TypeScript REPL by running tsx with no arguments.

npx tsx

Cache

Modules transformations are cached in the system cache directory (TMPDIR). Transforms are cached by content hash so duplicate dependencies are not re-transformed.

Set the --no-cache flag to disable the cache:

npx tsx --no-cache ./file.ts

Node.js Loader

tsx is a standalone binary used in-place of Node.js, but sometimes you'll want to use node directly. For example, when adding TypeScript & ESM support to Node.js binaries.

To use tsx with Node.js, pass it into the --loader flag.

Note: Node.js's experimental feature warnings will not be suppressed when used as a loader

# As a CLI flag
node --loader tsx ./file.ts

# As an environment variable
NODE_OPTIONS='--loader tsx' node ./file.ts

If you're looking to use the -r, --require flag, you can use @esbuild-kit/cjs-loader but transformations will not be applied to dynamic import()s.

Dependencies

FAQ

Does it do type-checking?

No, esbuild does not support type checking.

It's recommended to run TypeScript separately as a command (tsc --noEmit) or via IDE IntelliSense.

How is tsx different from ts-node?

They are both tools to run TypeScript files.

The main difference is that tsx is powered by esbuild for blazing fast TypeScript compilation. Whereas ts-node uses the TypeScript compiler, which is not as fast.

Because esbuild doesn't do type checking, tsx is more equivalent to ts-node --transpileOnly.

If you migrated from ts-node, please share your performance gains here!

Keywords

FAQs

Package last updated on 19 May 2022

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc