FZF Fuzzy Matching Algorithm for JavaScript
FZF for JavaScript is a library that ports the core fuzzy-finding algorithm from the popular FZF CLI tool into a JavaScript-native implementation, making it suitable for both browser and Node.js environments. The current stable version is `v0.5.2`. The project exhibits an active development cadence, with frequent minor releases that, while in `0.x` territory, introduce significant features and occasionally breaking changes. It differentiates itself by offering two distinct fuzzy algorithms (`v1` for speed, `v2` for better highlighting), support for extended search syntax, and an asynchronous finder for maintaining a responsive user interface during complex queries. This library is specifically designed to power command palettes and similar interactive search UIs in modern web applications, providing a robust and performant fuzzy matching experience.
Common errors
-
Cannot find module 'fzf' or its corresponding type declarations.
cause Incorrect TypeScript `moduleResolution` settings (e.g., `nodenext`, `node16`, `bundler`) in `tsconfig.json` when using `fzf` versions older than `v0.5.2`, or a missing `fzf` installation.fixEnsure `fzf` is installed (`npm i fzf`) and upgrade to `fzf@0.5.2` or later. If upgrading is not feasible, adjust `compilerOptions.moduleResolution` in `tsconfig.json` to `node`. -
TypeError: Fzf is not a constructor
cause Attempting to import `Fzf` using CommonJS `require()` syntax, but the package is primarily designed for ESM or the default export is not correctly handled in CJS environments.fixUse ESM `import { Fzf } from 'fzf'` instead of `const { Fzf } = require('fzf')`. Ensure your project is configured for ESM (e.g., `"type": "module"` in `package.json`). -
Error: Package "fzf" (v0.5.0) requires Node.js "^16" and npm "^8".
cause Attempting to use `fzf@0.5.0` (which was removed from NPM) in an environment that does not meet its specific, higher Node.js and NPM version requirements.fixThis specific version is known to be problematic. Upgrade to `fzf@0.5.1` or the latest stable version. Avoid using `fzf@0.5.0` entirely.
Warnings
- breaking The behavior of `options.tiebreakers` was modified in v0.5.1 to align with the original FZF implementation. If you relied on the previous tiebreaker logic, you may need to adjust your options or expected sorting behavior.
- breaking Version 0.5.0 was critically bugged and required Node.js 16 and NPM 8, leading to it being removed from NPM. It should never be used.
- gotcha As a `0.x` version package, `fzf` reserves the right to introduce breaking changes in minor version increments (e.g., `0.3.x` to `0.4.x`). Always consult migration guides and release notes when updating.
- gotcha Older versions of `fzf` (prior to `v0.5.2`) had issues with TypeScript `moduleResolution` settings like `nodenext`, `node16`, or `bundler`, which could prevent types from being correctly resolved.
- gotcha The default fuzzy algorithm was changed to 'v2' in v0.4.1 for better highlighting, but 'v1' is faster. Performance-sensitive applications might need to explicitly configure the algorithm.
Install
-
npm install fzf -
yarn add fzf -
pnpm add fzf
Imports
- Fzf
const { Fzf } = require('fzf')import { Fzf } from 'fzf' - FzfOptions
import type { FzfOptions } from 'fzf' - FzfResultItem
import type { FzfResultItem } from 'fzf'
Quickstart
import { Fzf } from 'fzf';
const programmingLanguages = [
'go', 'javascript', 'python', 'rust',
'swift', 'kotlin', 'elixir', 'java',
'lisp', 'v', 'zig', 'nim', 'rescript',
'd', 'haskell'
];
// Create a new Fzf instance with your list of items
const fzf = new Fzf(programmingLanguages, {
tiebreakers: ['end', 'length'], // Example option, uses 'end' and 'length' as secondary sort keys
limit: 5 // Limit the number of results
});
// Perform a fuzzy search
const query = 'jav';
const entries = fzf.find(query);
console.log(`Fuzzy search results for '${query}':`);
entries.forEach(entry => {
console.log(`- ${entry.item} (Score: ${entry.score})`);
});
// Expected output: javascript, java (order depends on scores and tiebreakers)