unist-util-position
unist-util-position is a crucial utility within the unified (unist) ecosystem, designed to robustly extract positional information from Unist (Universal Syntax Tree) nodes. This includes `start` and `end` points, along with `line`, `column`, and `offset` details. It's particularly valuable when processing trees that might have inconsistent or incomplete positional data due to user modifications or external plugins, acting as a safeguard against malformed data. The current stable version is 5.0.0, which mandates Node.js 16 or newer and operates exclusively as an ES module. New major versions are typically released to align with Node.js EOL policies and introduce significant breaking changes, while minor releases provide features and fixes. Its key differentiator is its focus on providing reliable positional data, returning `undefined` for invalid inputs rather than potentially throwing errors or returning partial, incorrect data, making it safer for robust plugin development compared to directly accessing `node.position` properties.
Common errors
-
Error [ERR_REQUIRE_ESM]: require() of ES Module .../node_modules/unist-util-position/index.js from ... not supported.
cause Attempting to import `unist-util-position` (which is ESM-only) using CommonJS `require()` syntax.fixChange your import statements from `const pkg = require('pkg')` to `import pkg from 'pkg'` or `import { namedExport } from 'pkg'`, and ensure your project/file is configured for ESM. -
TypeError: position is not a function
cause Incorrectly trying to access a named export after using `require('unist-util-position')` or attempting to destructure `require`'s result as if it were an ESM module.fixUse proper ES module named imports: `import { position, pointStart, pointEnd } from 'unist-util-position'`. -
Error: This module uses Node.js 16 or later features and cannot be run on Node.js 14.
cause Running `unist-util-position` v5.0.0 or higher on an unsupported Node.js version (e.g., Node.js 14).fixUpgrade your Node.js runtime environment to version 16 or a more recent, actively maintained version. -
TypeError: Cannot read properties of undefined (reading 'line')
cause Attempting to access properties (like `start.line`) on the return value of `position`, `pointStart`, or `pointEnd` without checking if the result is `undefined` (a new behavior in v5.0.0).fixAdd a check for `undefined` before accessing properties, e.g., `const pos = position(node); if (pos) { console.log(pos.start.line); }`.
Warnings
- breaking Version 5.0.0 and later require Node.js 16 or a newer compatible version. Running on older Node.js runtimes will result in runtime errors.
- breaking Starting with version 4.0.0, unist-util-position is an ES module (ESM) only. CommonJS `require()` is no longer supported.
- breaking As of v5.0.0, the `position`, `pointStart`, and `pointEnd` functions now return `undefined` for invalid nodes or positions, instead of potentially throwing an error or returning partial data. Consumers must explicitly check for `undefined`.
- breaking Version 5.0.0 changes to use `exports` map in `package.json`. This means direct imports to internal, non-public paths are no longer supported and will break.
- gotcha When upgrading to `unist-util-position` v5.0.0, ensure that you also update your `@types/unist` dependency to a compatible version to avoid potential type mismatches or errors, as v5 updated its internal `@types/unist` dependency.
Install
-
npm install unist-util-position -
yarn add unist-util-position -
pnpm add unist-util-position
Imports
- position
const { position } = require('unist-util-position')import { position } from 'unist-util-position' - pointStart
import pointStart from 'unist-util-position/pointStart'
import { pointStart } from 'unist-util-position' - pointEnd
const position = require('unist-util-position').positionimport { pointEnd } from 'unist-util-position'
Quickstart
import {fromMarkdown} from 'mdast-util-from-markdown'
import {pointEnd, pointStart, position} from 'unist-util-position'
const markdown = '# Hello\n\n* World\n'
const tree = fromMarkdown(markdown)
// Get the full position of the tree
const fullPosition = position(tree)
console.log('Full Position:', fullPosition)
// Expected: { start: { line: 1, column: 1, offset: 0 }, end: { line: 4, column: 1, offset: 13 } }
// Get the starting point of the tree
const startPoint = pointStart(tree)
console.log('Start Point:', startPoint)
// Expected: { line: 1, column: 1, offset: 0 }
// Get the ending point of the tree
const endPoint = pointEnd(tree)
console.log('End Point:', endPoint)
// Expected: { line: 4, column: 1, offset: 13 }
// Example of handling undefined for invalid nodes (v5+ behavior)
const invalidNode = null
const invalidPosition = position(invalidNode)
console.log('Invalid Position:', invalidPosition)
// Expected: undefined - always check for undefined with v5+