dprint-node: Fast Code Formatter API for Node.js

1.0.8 · active · verified Tue Apr 21

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

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to format a TypeScript code string using `dprint-node` with custom configuration options for line width, semi-colons, indentation, and quote style, logging both the original and formatted output.

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);
}

view raw JSON →