ripgrep-node

0.3.1 · active · verified Wed Apr 22

This `ripgrep` npm package provides the powerful command-line utility `ripgrep` in a fully cross-platform, dependency-free manner by compiling it to WebAssembly (WASM). As of version 0.3.1, it offers programmatic access to `ripgrep`'s core functionality, suitable for Node.js, Bun, Deno, and even bundler-friendly browser environments without requiring native binaries or post-install scripts. The package embeds the WASM module as a z85+brotli encoded string and leverages WASI for execution, defaulting to Node.js's built-in `node:wasi` for optimal performance when available. Its key differentiator is providing `ripgrep`'s capabilities entirely within the JavaScript ecosystem, making it a drop-in programmatic replacement for native `ripgrep` spawns, with the WASM module dynamically cached to the OS temp directory for subsequent fast access.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates programmatic usage of `ripgrep` to search a temporary file, capturing its output, and an alternative method using `rgPath` with `node:child_process.spawn`.

import { ripgrep, rgPath } from "ripgrep";
import { spawn } from "node:child_process";
import * as fs from 'node:fs';
import * as path from 'node:path';

// Create a dummy file for searching
const tempDir = fs.mkdtempSync(path.join(process.cwd(), 'rg-temp-'));
const filePath = path.join(tempDir, 'test.txt');
fs.writeFileSync(filePath, 'This is a test with TODO item one.\nAnother line with TODO item two.');

async function runRipgrep() {
  console.log('--- Running ripgrep programmatically (buffer output) ---');
  try {
    const { code, stdout, stderr } = await ripgrep(["TODO", tempDir], { buffer: true });
    console.log(`Exit Code: ${code}`);
    console.log(`STDOUT:\n${stdout}`);
    console.log(`STDERR:\n${stderr}`);
  } catch (e) {
    console.error('Error running ripgrep programmatically:', e);
  }

  console.log('\n--- Spawning ripgrep as a child process ---');
  try {
    const child = spawn(rgPath, ["TODO", tempDir], { stdio: "pipe" });
    child.stdout.pipe(process.stdout);
    child.stderr.pipe(process.stderr);
    child.on('close', (code) => {
      console.log(`Child process exited with code ${code}`);
      fs.rmSync(tempDir, { recursive: true, force: true }); // Clean up
    });
  } catch (e) {
    console.error('Error spawning ripgrep:', e);
    fs.rmSync(tempDir, { recursive: true, force: true }); // Clean up
  }
}

runRipgrep();

view raw JSON →