Unist Utility to Stringify Position
unist-util-stringify-position is a utility package within the unified (unist) ecosystem designed to consistently serialize positional information from abstract syntax tree (AST) nodes, positions, or points into a human-readable string format. This is particularly useful for generating error messages, warnings, or logging location-specific information in parsers, compilers, and transformers. The current stable version is 4.0.0, which was released recently and is actively maintained by the syntax-tree collective. Its release cadence is tied to breaking changes and necessary updates within the broader unist ecosystem. Key differentiators include its adherence to the unist specification, full TypeScript support, and its focus on standardizing how location data is presented to end-users, ensuring consistency across various unified processors like `remark` or `rehype`.
Common errors
-
ERR_REQUIRE_ESM
cause Attempting to `require` this ESM-only package in a CommonJS context.fixUse `import { stringifyPosition } from 'unist-util-stringify-position'` instead of `require`. Ensure your Node.js project or file is configured for ES modules (e.g., `"type": "module"` in `package.json`). -
TypeError: stringifyPosition is not a function
cause Incorrect CommonJS `require` attempt, often occurring when trying to destructure named exports from an ESM-only package without a default export.fixThis package exports named exports. If you're in an ESM context, use `import { stringifyPosition } from 'unist-util-stringify-position'`. If you're stuck in CommonJS, consider dynamically `import`ing or transpiling your code.
Warnings
- breaking Version 4.0.0 changes the minimum required Node.js version to 16 or later.
- breaking The package switched to ESM-only in v3.0.0 and further adopted `export` maps in v4.0.0, meaning CommonJS `require` statements are no longer supported for importing this package.
- gotcha Version 4.0.0 includes an update to its `@types/unist` dependency, which might cause type conflicts if your project uses an incompatible older version of `@types/unist`.
Install
-
npm install unist-util-stringify-position -
yarn add unist-util-stringify-position -
pnpm add unist-util-stringify-position
Imports
- stringifyPosition
const stringifyPosition = require('unist-util-stringify-position')import { stringifyPosition } from 'unist-util-stringify-position'
Quickstart
import { stringifyPosition } from 'unist-util-stringify-position';
console.log('Point:', stringifyPosition({line: 2, column: 3}));
// => '2:3'
console.log('Position:', stringifyPosition({start: {line: 2, column: 1}, end: {line: 3, column: 1}}));
// => '2:1-3:1'
console.log('Node:', stringifyPosition({
type: 'text',
value: '!',
position: {
start: {line: 5, column: 11},
end: {line: 5, column: 12}
}
}));
// => '5:11-5:12'
console.log('Invalid input:', stringifyPosition(null));
// => ''