Oxc Formatter (oxfmt)
Oxfmt is a high-performance, zero-configuration code formatter for JavaScript and TypeScript, built as part of the broader Oxidation Compiler (Oxc) project. It offers a fast alternative to tools like Prettier, leveraging Rust for superior performance. The package is currently at `v0.45.0` and is under active development, with frequent releases often tied to its companion linter, Oxlint, and the underlying Oxc Rust crates (typically weekly to bi-weekly for the monorepo). Its primary differentiators are its exceptional speed, minimal configuration overhead (enforcing an opinionated style), and its tight integration within the Oxc ecosystem, which aims to provide a complete suite of JavaScript tooling written in Rust. It ships with TypeScript types, supporting both programmatic usage via `formatSync` and `formatAsync` functions, and a direct command-line interface.
Common errors
-
Error: The module '\path\to\node_modules\oxfmt\oxfmt.node' was compiled against a different Node.js version using NODE_MODULE_VERSION N. This version of Node.js requires NODE_MODULE_VERSION M. Please update your Node.js installation or recompile the module.
cause The native Rust addon for `oxfmt` was built for a Node.js ABI version that is incompatible with the currently running Node.js runtime.fixUpgrade or downgrade your Node.js version to match the `engines` requirement specified in `oxfmt`'s `package.json` (`^20.19.0 || >=22.12.0`). Use `nvm` or `volta` to manage Node.js versions, and reinstall `oxfmt` after changing versions to ensure the correct native binary is used. -
command not found: oxfmt
cause The `oxfmt` executable is not in the system's PATH, or the package is not installed correctly for direct command-line access.fixUse `npx oxfmt@latest` to run the latest version without global installation. If you've installed it locally (`npm install oxfmt`), you can run it via `npx oxfmt` (which resolves from `node_modules/.bin`) or by adding `node_modules/.bin` to your PATH. -
TypeError: Cannot read properties of undefined (reading 'formatSync') at Object.<anonymous> (/path/to/your/script.js:L:C)
cause Attempting to access `formatSync` (or `formatAsync`) from the `oxfmt` package using incorrect import syntax for the module system (CommonJS vs. ESM) or when the symbol is not correctly exported/resolved.fixFor ESM projects, use named imports: `import { formatSync } from 'oxfmt';`. For CommonJS projects (Node.js versions without ESM support or older configurations), use `const { formatSync } = require('oxfmt');`. Verify that you are importing the correct symbol name.
Warnings
- breaking The `oxc` project undergoes rapid development, leading to frequent breaking changes in its underlying Rust crates (`oxc_allocator`, `oxc_span`, `oxc_str`, etc.). While `oxfmt`'s Node-API surface aims for stability, deep dependencies on `oxc`'s internal structures could lead to unexpected behavioral changes or errors with new `oxfmt` versions if the underlying `oxc` crates introduce incompatible changes.
- gotcha Oxfmt is designed for zero-configuration by default, meaning it has a strong opinion on formatting styles and provides very few options for customization compared to tools like Prettier. Users accustomed to fine-grained control over formatting rules may find this limiting.
- gotcha The `oxfmt` package requires specific Node.js versions due to its native (Rust-based) Node-API bindings. Running `oxfmt` on an unsupported Node.js version will result in a runtime error stating the module was compiled against a different Node.js ABI.
- gotcha As a performance-focused tool implemented in Rust, any unexpected runtime crashes or unhandled exceptions within `oxfmt` may manifest as native Node.js process crashes (e.g., segmentation faults), which can be harder to debug than typical JavaScript errors.
Install
-
npm install oxfmt -
yarn add oxfmt -
pnpm add oxfmt
Imports
- formatSync
const { formatSync } = require('oxfmt');import { formatSync } from 'oxfmt'; - formatAsync
const { formatAsync } = require('oxfmt');import { formatAsync } from 'oxfmt'; - FormatterOptions
import { FormatterOptions } from 'oxfmt';import type { FormatterOptions } from 'oxfmt';
Quickstart
import { formatSync, formatAsync } from 'oxfmt';
import { readFileSync, writeFileSync, rmSync } from 'node:fs';
import { join } from 'node:path';
// Example: Synchronously format a string
const messyCode = `function add ( a, b ) { return a + b; }`;
const formattedCodeSync = formatSync(messyCode);
console.log('Synchronously formatted code:', formattedCodeSync);
// Expected output: "function add(a, b) { return a + b; }"
// Example: Asynchronously format a file
async function formatFile(filePath: string) {
try {
const sourceText = readFileSync(filePath, 'utf-8');
const formattedText = await formatAsync(sourceText);
console.log(`Formatted ${filePath}:\n`, formattedText);
// Optionally write back to file
// writeFileSync(filePath, formattedText);
} catch (error) {
console.error(`Error formatting file ${filePath}:`, error);
}
}
// Create a dummy file for demonstration
const dummyFilePath = join(process.cwd(), 'temp-file.js');
writeFileSync(dummyFilePath, `
const foo = "bar" ;
if ( true ) { console.log(foo) ; }`);
console.log('Attempting to format a dummy file...');
formatFile(dummyFilePath).then(() => {
console.log('Formatting complete for dummy file.');
// Clean up dummy file
rmSync(dummyFilePath);
});