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.
npm install swc-nodeVerified import paths — ran on the pinned version, not inferred.
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.
Migrate to using `node -r @swc-node/register` for CommonJS or `node --import @swc-node/register/esm-register` for ES Modules, along with `@swc-node/register` as a direct dependency.
Always run `tsc --noEmit` or integrate TypeScript's own type checker into your CI/CD pipeline or development workflow to ensure type safety, in addition to using SWC for fast transpilation.
Ensure your Node.js installation matches your operating system's architecture. On Windows, install the latest Microsoft Visual C++ Redistributables. If using `npm`, update it to the latest version and regenerate lockfiles to ensure optional dependencies are correctly installed.
Carefully review SWC's documentation on decorators and `tsconfig.json` compatibility. Ensure `isolatedModules: true` and potentially `verbatimModuleSyntax: true` are set in `tsconfig.json` to flag code that might not transpile correctly without type-system awareness.
Ensure `package.json` contains `"type": "module"` for ESM projects. Use explicit file extensions (e.g., `.js` in import paths for compiled output) and verify Node.js version compatibility with ESM features. Update `@swc-node/register` to the latest version, as ESM-related fixes are ongoing.
Verify your Node.js installation (e.g., `node -p "process.arch"`) matches your OS architecture. On Windows, install the Microsoft Visual C++ Redistributables. If using npm, try `npm cache clean --force` then `rm -rf node_modules package-lock.json` and `npm install` again to force a fresh download of optional platform-specific binaries.
Ensure your `package.json` has `"type": "module"`. Use the command `node --import @swc-node/register/esm-register your-script.ts`. If using a test runner, verify its configuration for ESM and the SWC register hook. Update `@swc-node/register` to its latest version.
In your `.swcrc` configuration (or `jsc` options), ensure `jsc.parser.syntax` is `"typescript"` and `jsc.parser.decorators` is `true`. If using legacy decorators, also set `jsc.transform.legacyDecorator: true`. Also, verify `experimentalDecorators` and `emitDecoratorMetadata` are set in `tsconfig.json` if applicable.
Run `npm install @swc-node/register` or `yarn add @swc-node/register` to add it to your `devDependencies`. Ensure your `node -r` or `node --import` command correctly references the installed package.