SWC Node.js CLI

1.0.0 · abandoned · verified Sun Apr 19

swc-node is a command-line interface (CLI) wrapper for `@swc-node/register`, a high-performance TypeScript and JavaScript transpiler designed for Node.js environments. While the `swc-node` package itself is at version `1.0.0` and has not seen recent updates (last published over four years ago), its core dependency, `@swc-node/register`, is actively developed, with its latest stable version being `1.11.0`. This library leverages SWC (Speedy Web Compiler), a Rust-based tool, to provide significantly faster compilation of TypeScript and modern JavaScript to a compatible target, often serving as a performant alternative to `ts-node` for development workflows. It focuses purely on transpilation without performing type checking, making it ideal for environments where build speed is prioritized and type checking is handled by a separate process (e.g., `tsc`). It supports both CommonJS via a `require` hook and ES Modules using Node.js's `--import` flag. Key differentiators include its Rust-native speed, minimal overhead, and `tsconfig.json` compatibility for transpilation options.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to set up a basic TypeScript project and run it using the `swc-node` CLI, as well as directly via the `@swc-node/register` CommonJS and ES Module runtime hooks.

{
  "name": "my-swc-project",
  "version": "1.0.0",
  "description": "A simple SWC project demonstrating swc-node usage",
  "main": "src/index.ts",
  "type": "module",
  "scripts": {
    "start": "npx swc-node src/index.ts",
    "start-cjs": "node -r @swc-node/register src/index.ts",
    "start-esm": "node --import @swc-node/register/esm-register src/index.ts"
  },
  "devDependencies": {
    "swc-node": "^1.0.0",
    "@swc-node/register": "^1.11.0",
    "typescript": "^5.0.0"
  }
}

// tsconfig.json
{
  "compilerOptions": {
    "target": "es2020",
    "module": "esnext",
    "lib": ["es2020", "dom"],
    "strict": true,
    "esModuleInterop": true,
    "isolatedModules": true,
    "forceConsistentCasingInFileNames": true,
    "skipLibCheck": true
  },
  "include": ["src/**/*.ts"]
}

// src/index.ts
import { greet } from './util.js'; // Note .js extension for ESM resolution

console.log(greet('Developer'));

async function simulateAsyncCall() {
  console.log('Starting async operation...');
  await new Promise(resolve => setTimeout(resolve, 1000));
  console.log('Async operation complete!');
}

simulateAsyncCall();

// src/util.ts
export function greet(name: string): string {
  return `Hello, ${name}! This message was transpiled by SWC.`;
}

view raw JSON →