vfile-location is a utility package within the unified ecosystem, currently at stable version 5.0.3, designed to convert between positional (line and column-based) and offset (character index-based) locations within a vfile instance. It provides `toOffset` and `toPoint` methods for accurate translation. The package sees active development and frequent, minor releases, with major versions typically introducing breaking changes like the move to ESM-only and Node.js 16+ requirement in v5. A key differentiator is its tight integration with `vfile`, making it ideal for tasks like generating precise linting reports or manipulating text based on raw file content where traditional AST node locations might be insufficient for location tracking.
npm install vfile-locationVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates how to create a location index for a `VFile`, convert a line/column point to a character offset, and convert an offset back to a point, including how out-of-bounds values are handled since v5.
Migrate your project to use ES modules (`import`/`export`) or use a bundler that can handle ESM imports. Update your Node.js version if necessary (Node.js 16+ is required).
Upgrade your Node.js runtime to version 16 or newer. For projects that must support older Node.js versions, consider pinning to `vfile-location@^4`.
Update your code to check for `undefined` instead of `-1` when validating the return value of `toOffset`. For example, use `if (offset === undefined)`.
Update your code to explicitly check for `undefined` when validating the return value of `toPoint`. For example, `if (point === undefined)`.
Always use `import { location } from 'vfile-location'`.Update your module to use ES module syntax (`import { location } from 'vfile-location'`) and ensure your project is configured for ESM, or use a bundler.Ensure you are using a named import: `import { location } from 'vfile-location'`.Verify that `VFile` is correctly imported and initialized before passing it to `location()`. Also, double-check the `location` import syntax as detailed in `imports`.