vfile-find-up
vfile-find-up is a utility within the vfile ecosystem designed to locate one or more files by traversing the file system upwards from a specified path. It operates on `vfile` objects, which standardize virtual file representation, making it suitable for unified processing workflows. The current stable version is 7.1.0, and the package maintains a steady release cadence, typically introducing major versions for breaking API changes or significant architectural shifts. Key differentiators include its tight integration with `vfile` for consistent file handling, its specific focus on *upward* directory traversal (contrasting with `vfile-find-down`), and its provision of both promise-based and traditional callback APIs, offering flexibility for asynchronous operations. It's particularly useful for locating configuration files or project roots.
Common errors
-
Error [ERR_REQUIRE_ESM]: require() of ES module /path/to/node_modules/vfile-find-up/index.js from /your/project/index.js not supported.
cause Attempting to import `vfile-find-up` using CommonJS `require()` syntax in a non-ESM environment.fixSwitch to ES Module `import` syntax (`import { findUp } from 'vfile-find-up';`) and ensure your Node.js project is configured for ESM. -
TypeError: vfile_find_up_1.findUp is not a function
cause After `v7.0.0`, the `findUp` function (which found all files) was renamed to `findUpAll`, and the old `findUpOne` became `findUp` (finding the first file).fixIf you were looking for all files, change `findUp` to `findUpAll`. If you were using `findUpOne`, change it to `findUp`. Check the API changes for `v7.0.0`. -
Error: vfile-find-up requires Node.js 16 or later.
cause Running `vfile-find-up@7.0.0` or later on an unsupported Node.js version (older than 16).fixUpgrade your Node.js environment to version 16 or higher to meet the package's minimum requirements.
Warnings
- breaking Version 7.0.0 changed the API names. The previous `findUp` (which found all files) was renamed to `findUpAll`, and the previous `findUpOne` (which found the first file) was renamed to `findUp`.
- breaking Version 7.0.0 updated the expected return value for custom `test` functions. Instead of bitwise symbols (e.g., `EXIT`, `INCLUDE`), you must now return a plain object like `{ break: true, include: true }`.
- breaking Version 7.0.0 increased the minimum Node.js requirement to Node.js 16 or higher.
- breaking Version 6.0.0 made the package ESM-only. CommonJS `require()` is no longer supported.
- gotcha This package returns `VFile` objects, but their `value` property (file content) is *not* populated by default. If you need to read the file content, you must explicitly use a utility like `to-vfile`.
Install
-
npm install vfile-find-up -
yarn add vfile-find-up -
pnpm add vfile-find-up
Imports
- findUp
const findUp = require('vfile-find-up');import { findUp } from 'vfile-find-up'; - findUpAll
import findUpAll from 'vfile-find-up';
import { findUpAll } from 'vfile-find-up'; - Callback
import { Callback } from 'vfile-find-up';import type { Callback } from 'vfile-find-up';
Quickstart
import { findUp, findUpAll } from 'vfile-find-up';
import { toVFile } from 'to-vfile'; // Recommended for reading file content
async function runFindUpExamples() {
const currentDir = process.cwd();
// Find the first package.json upwards
console.log('--- Finding single package.json ---');
try {
const pkgFile = await findUp('package.json', currentDir);
if (pkgFile) {
console.log(`Found package.json at: ${pkgFile.path}`);
// To read its content, use to-vfile
const fullPkgFile = await toVFile.read(pkgFile.path);
console.log('Content (first 100 chars):', fullPkgFile.value?.toString().substring(0, 100) + '...');
} else {
console.log('No package.json found upwards.');
}
} catch (error) {
console.error('Error finding package.json:', error);
}
// Find all .md files upwards (demonstrating custom test function)
console.log('\n--- Finding all .md files upwards ---');
try {
const mdFiles = await findUpAll((file) => file.extname === '.md', currentDir);
if (mdFiles.length > 0) {
console.log(`Found ${mdFiles.length} markdown files:`);
mdFiles.forEach((file) => console.log(`- ${file.path}`));
} else {
console.log('No markdown files found upwards.');
}
} catch (error) {
console.error('Error finding markdown files:', error);
}
}
runFindUpExamples();