dprint-node: Fast Code Formatter API for Node.js
The `dprint-node` package provides a high-performance Node.js API for integrating the `dprint` TypeScript and JavaScript code formatter into applications. Leveraging Rust via `napi-rs`, it offers significantly faster formatting speeds compared to traditional JavaScript-based formatters like Prettier, a key differentiator showcased in its benchmarks. The library is currently stable at version 1.0.8, with a focus on active maintenance, indicated by consistent patch releases addressing bug fixes and internal dependency updates. Its core functionality involves a simple `dprint.format` method, which accepts a file path, the code string, and an optional configuration object, enabling developers to programmatically apply `dprint`'s extensive formatting rules for TypeScript, JavaScript, and JSX/TSX syntax. Users can customize formatting behavior by passing configuration options that align with the `dprint` plugin documentation.
Common errors
-
Error: Cannot find module 'dprint-node'
cause The 'dprint-node' package is not installed or not resolvable in the current environment.fixRun `npm install dprint-node` or `yarn add dprint-node` to add the package to your project dependencies. -
TypeError: dprint.format is not a function
cause This typically occurs when trying to destructure the `dprint` object or using an incorrect import/require syntax that does not yield the expected default export object with the `format` method.fixEnsure you are using `const dprint = require('dprint-node');` for CommonJS or `import dprint from 'dprint-node';` for ES Modules, as the library exports its primary functionality as a default object. -
Property 'someInvalidOption' does not exist on type 'object' (or similar type error for config)
cause You are passing a configuration option that is not recognized or correctly typed by the `dprint` formatter's schema for the given file type, or your TypeScript environment cannot correctly infer the type.fixConsult the official `dprint` plugin configuration documentation (e.g., `dprint.dev/plugins/typescript/config/`) for valid options. Ensure the option name and value type match the expected schema.
Warnings
- gotcha Versions prior to `1.0.5` had missing type declarations, which could lead to TypeScript compilation errors for users leveraging the library's types.
- breaking Versions prior to `1.0.3` could experience crashes on Apple Silicon (M1/M2) machines due to issues with the underlying `napi-rs` bindings.
- gotcha The configuration options for `dprint.format`'s third parameter are defined externally by `dprint` plugins (e.g., TypeScript plugin config on dprint.dev). `dprint-node` itself might not provide granular, explicitly exported TypeScript types for all possible configuration properties, meaning misconfigured options might only manifest as runtime errors.
Install
-
npm install dprint-node -
yarn add dprint-node -
pnpm add dprint-node
Imports
- dprint
const dprint = require('dprint-node'); - dprint
import dprint from 'dprint-node';
- Formatting Configuration
import { Configuration } from 'dprint-node';import type { Configuration } from 'dprint-node/types';
Quickstart
import dprint from 'dprint-node';
const filePath = './src/example.ts';
const unformattedCode = `
function greet( name : string ) {
console.log( `Hello, ${ name }!` )
}
`;
try {
const formattedCode = dprint.format(filePath, unformattedCode, {
lineWidth: 120,
semiColons: "always",
indentWidth: 2,
quoteStyle: "alwaysSingle"
});
console.log('Original code:\n', unformattedCode);
console.log('\nFormatted code:\n', formattedCode);
} catch (error) {
console.error("Error formatting code:", error.message);
}