Oxc Transform Node API
oxc-transform is the Node.js API for Oxc's high-performance JavaScript, TypeScript, and React JSX transformer, which is built in Rust. It provides synchronous (`transformSync`, `isolatedDeclarationSync`) and asynchronous (`transform`, `isolatedDeclaration`) functions for parsing and transforming code. A key feature is its specialized capability for generating TypeScript declaration files (`.d.ts`) with isolated declarations, fully conforming to TypeScript's `--isolatedDeclarations` compiler option. Currently at version 0.126.0, the package maintains a rapid release cadence, reflecting active and continuous development within the broader Oxc project ecosystem. Its primary differentiators include its Rust-native performance, positioning it as a fast and efficient alternative to tools like Babel, and its robust, up-to-date support for modern ECMAScript features, TypeScript syntax, and React JSX out-of-the-box. The library is designed to serve as a foundational, high-performance component for various JavaScript build and tooling pipelines requiring efficient code transformation.
Common errors
-
Error: The module '\path\to\node_modules\oxc-transform\index.node' was compiled against a different Node.js version. This version of Node.js cannot load modules compiled against Node.js API version X.
cause The `oxc-transform` native module was built for a different Node.js ABI version than the one currently running, typically due to Node.js version mismatch.fixEnsure your Node.js version meets the `oxc-transform` requirements (`^20.19.0 || >=22.12.0`). If you have multiple Node.js versions, switch to the correct one using `nvm use` or similar version manager. You may need to reinstall `oxc-transform` to rebuild it against your current Node.js ABI: `npm rebuild oxc-transform` or `npm install`. -
ReferenceError: require is not defined in ES module scope
cause Attempting to use CommonJS `require` syntax in an ES Module context (`type: "module"` in `package.json` or `.mjs` file) for `oxc-transform`.fixUse ES Module `import` syntax. Change `const { transformSync } = require('oxc-transform');` to `import { transformSync } from 'oxc-transform';`.
Warnings
- breaking The `oxc-transform` package now requires Node.js versions `>=20.19.0` or `>=22.12.0`. Older Node.js versions are not supported and will fail to load the native module.
- breaking Changes in internal `oxc_span` crate may affect TypeScript type re-exports. Specifically, identity `FromIn` implementation for `Ident` and re-exports of string types from `oxc_span` were removed, and a `static_ident!` macro was added. If your project relied on specific internal types re-exported by `oxc-transform`'s type definitions, these might now be unavailable or altered.
Install
-
npm install oxc-transform -
yarn add oxc-transform -
pnpm add oxc-transform
Imports
- transformSync
const { transformSync } = require('oxc-transform');import { transformSync } from 'oxc-transform'; - transform
const { transform } = require('oxc-transform');import { transform } from 'oxc-transform'; - isolatedDeclarationSync
const { isolatedDeclarationSync } = require('oxc-transform');import { isolatedDeclarationSync } from 'oxc-transform'; - TransformOptions
import type { TransformOptions } from 'oxc-transform';
Quickstart
import assert from "assert";
import { transformSync } from "oxc-transform";
const filename = "test.ts";
const sourceCode = "class A<T> { constructor(arg: T) { console.log(arg); } }";
// Transform TypeScript code to plain JavaScript and generate a declaration file
const { code, declaration, errors } = transformSync(filename, sourceCode, {
typescript: {
declaration: true, // Enable isolated declarations
jsxPragma: "React",
jsxPragmaFrag: "React.Fragment"
},
// Example: Enable more transformations if needed
react: {
runtime: "automatic",
development: true
}
});
// Log results for verification
console.log("Transformed Code:\n", code);
console.log("Declaration File:\n", declaration);
// Basic assertions
assert.equal(code.trim(), "class A { constructor(arg) { console.log(arg); } }");
assert.equal(declaration.trim(), "declare class A<T> {\n constructor(arg: T);\n}");
assert.equal(errors.length, 0, `Expected no errors, but got ${errors.length} errors.`);
console.log("Transformation successful!");