Unist Utility to Remove Position Information
unist-util-remove-position is a specialized utility within the unified ecosystem, designed to strip the 'position' field from nodes within a unist Abstract Syntax Tree (AST). This is particularly useful when comparing trees, merging them, or when positional information is no longer relevant, such as after processing or when the AST is being used for structural analysis where source location is a distraction. The package is currently at stable version 5.0.0 and is actively maintained by the unified collective, aligning with their standard release cadence that typically drops support for unmaintained Node.js versions with new major releases. It differentiates itself by offering a focused solution for position removal, contrasting with related utilities that extract or stringify position data.
Common errors
-
Error [ERR_REQUIRE_ESM]: require() of ES Module /path/to/node_modules/unist-util-remove-position/index.js not supported.
cause Attempting to import an ESM-only package using CommonJS `require()` in a non-ESM context.fixUpdate your import statement to `import { removePosition } from 'unist-util-remove-position'` and ensure your Node.js project or file is configured for ES Modules (e.g., `"type": "module"` in `package.json`). -
TypeError: removePosition is not a function
cause Incorrect import syntax for an ESM module, or attempting to access a named export as a default export, or a CommonJS `require()` failure.fixUse `import { removePosition } from 'unist-util-remove-position'` for ESM. Double-check your environment is correctly configured for ESM if you're getting `ERR_REQUIRE_ESM`. -
TypeError: Argument of type 'boolean' is not assignable to parameter of type 'Options | undefined'.
cause Using the old `force: true` shortcut with `removePosition(node, true)` after upgrading to v5.0.0.fixUpdate the call to `removePosition(node, { force: true })` to use the new options object syntax. -
Node.js version X is not supported by 'unist-util-remove-position@5.0.0'.
cause Running the package with an unsupported Node.js version (specifically, older than 16.0).fixUpgrade your Node.js environment to version 16 or newer. Alternatively, if forced to use an older Node.js, downgrade to `unist-util-remove-position@^4`.
Warnings
- breaking Version 5.0.0 changes to require Node.js 16 or higher. Older Node.js versions are no longer supported.
- breaking Version 5.0.0 removed the `force` shortcut. Previously, `removePosition(tree, true)` would work. Now, the `force` option must be passed as an object: `{ force: true }`.
- breaking Version 5.0.0 changed the return value of `removePosition` to `undefined`. Previously, it might have returned the `node` itself.
- breaking Version 4.0.0 switched to being an ESM-only package. It no longer supports CommonJS `require()` syntax.
- breaking Version 3.0.0 introduced TypeScript types. While generally beneficial, it could cause compilation errors if your TypeScript configuration or dependent types are incompatible.
Install
-
npm install unist-util-remove-position -
yarn add unist-util-remove-position -
pnpm add unist-util-remove-position
Imports
- removePosition
const removePosition = require('unist-util-remove-position')import { removePosition } from 'unist-util-remove-position' - Options
import type { Options } from 'unist-util-remove-position'
Quickstart
import {fromMarkdown} from 'mdast-util-from-markdown'
import {removePosition} from 'unist-util-remove-position'
// Create a sample Markdown AST with positional information
const markdown = 'Some _emphasis_, **importance**, and `code`.'
const tree = fromMarkdown(markdown)
console.log('Tree with positions:')
console.dir(tree, {depth: null})
// Remove positional information from the tree, forcing deletion of fields
removePosition(tree, {force: true})
console.log('\nTree without positions:')
console.dir(tree, {depth: null})