{"id":16008,"library":"dprint-node","title":"dprint-node: Fast Code Formatter API for Node.js","description":"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.","status":"active","version":"1.0.8","language":"javascript","source_language":"en","source_url":"https://github.com/devongovett/dprint-node","tags":["javascript","typescript"],"install":[{"cmd":"npm install dprint-node","lang":"bash","label":"npm"},{"cmd":"yarn add dprint-node","lang":"bash","label":"yarn"},{"cmd":"pnpm add dprint-node","lang":"bash","label":"pnpm"}],"dependencies":[],"imports":[{"note":"CommonJS import for the main dprint object.","symbol":"dprint","correct":"const dprint = require('dprint-node');"},{"note":"ES Modules default import. This is common for packages shipping types and a single primary export.","symbol":"dprint","correct":"import dprint from 'dprint-node';"},{"note":"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.","wrong":"import { Configuration } from 'dprint-node';","symbol":"Formatting Configuration","correct":"import type { Configuration } from 'dprint-node/types';"}],"quickstart":{"code":"import dprint from 'dprint-node';\n\nconst filePath = './src/example.ts';\nconst unformattedCode = `\nfunction  greet( name :  string ) { \n  console.log( `Hello, ${ name }!` )\n}\n`;\n\ntry {\n  const formattedCode = dprint.format(filePath, unformattedCode, {\n    lineWidth: 120,\n    semiColons: \"always\",\n    indentWidth: 2,\n    quoteStyle: \"alwaysSingle\"\n  });\n  console.log('Original code:\\n', unformattedCode);\n  console.log('\\nFormatted code:\\n', formattedCode);\n} catch (error) {\n  console.error(\"Error formatting code:\", error.message);\n}","lang":"typescript","description":"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."},"warnings":[{"fix":"Upgrade to `dprint-node@1.0.5` or later to ensure complete type declarations are available.","message":"Versions prior to `1.0.5` had missing type declarations, which could lead to TypeScript compilation errors for users leveraging the library's types.","severity":"gotcha","affected_versions":"<1.0.5"},{"fix":"Update to `dprint-node@1.0.3` or newer to resolve the stability issues on Apple Silicon devices.","message":"Versions prior to `1.0.3` could experience crashes on Apple Silicon (M1/M2) machines due to issues with the underlying `napi-rs` bindings.","severity":"breaking","affected_versions":"<1.0.3"},{"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.","message":"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.","severity":"gotcha","affected_versions":">=1.0.0"}],"env_vars":null,"last_verified":"2026-04-21T00:00:00.000Z","next_check":"2026-07-20T00:00:00.000Z","problems":[{"fix":"Run `npm install dprint-node` or `yarn add dprint-node` to add the package to your project dependencies.","cause":"The 'dprint-node' package is not installed or not resolvable in the current environment.","error":"Error: Cannot find module 'dprint-node'"},{"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.","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.","error":"TypeError: dprint.format is not a function"},{"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.","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.","error":"Property 'someInvalidOption' does not exist on type 'object' (or similar type error for config)"}],"ecosystem":"npm"}