Registry / serialization / dprint-node

dprint-node

JSON →
library1.0.8jsnpmunverified

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.

npm install dprint-node
INSTALL
IMPORT
SIG · DPRINT-NODE
D
dprint-node
serializationjavascriptv1.0.8
Install
Import
Disk
Pass rate
0/ 6
Env Coverage0 / 6
glibc
1822
musl
1822
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
node 18226 runs
build_error
glibc
node 18226 runs
build_error
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

dprint
const dprint = require('dprint-node');
CommonJS import for the main dprint object.
dprint
import dprint from 'dprint-node';
ES Modules default import. This is common for packages shipping types and a single primary export.
Formatting Configuration
import type { Configuration } from 'dprint-node/types';
import { Configuration } from 'dprint-node';
While dprint-node ships types, the explicit Configuration type might be located in a subpath or inferred by the `dprint.format` function's third argument. Refer to dprint.dev for detailed config options, as `dprint-node` acts as a wrapper. For robust typing, consider defining a local interface or consulting the package's `.d.ts` files.

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); }
Debug
Known issues
gotchaVersions prior to `1.0.5` had missing type declarations, which could lead to TypeScript compilation errors for users leveraging the library's types.
fix
Upgrade to `dprint-node@1.0.5` or later to ensure complete type declarations are available.
affects: <1.0.5
breakingVersions prior to `1.0.3` could experience crashes on Apple Silicon (M1/M2) machines due to issues with the underlying `napi-rs` bindings.
fix
Update to `dprint-node@1.0.3` or newer to resolve the stability issues on Apple Silicon devices.
affects: <1.0.3
gotchaThe 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.
fix
Always refer to the official `dprint` plugin configuration documentation (e.g., `dprint.dev/plugins/typescript/config/`) to ensure correct and valid options are passed. Consider creating a local TypeScript interface for your configuration objects based on the dprint schema for better type safety.
affects: >=1.0.0
Errors
Common errors & fixes
Error: Cannot find module 'dprint-node'
The 'dprint-node' package is not installed or not resolvable in the current environment.
fix
Run `npm install dprint-node` or `yarn add dprint-node` to add the package to your project dependencies.
TypeError: dprint.format is not a function
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.
fix
Ensure 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)
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.
fix
Consult 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.
Upgrade
Version history
1.0.8latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
4 hits · last 30 days
node
4
Resources
dprint-node — npm install dprint-node · libregistry