Node.js bindings for Hnswlib
hnswlib-node provides Node.js bindings to the HNSWlib C++ library, enabling fast approximate nearest neighbor (ANN) search using Hierarchical Navigable Small World (HNSW) graphs. This package is essential for JavaScript developers building recommendation engines, semantic search systems, or any application requiring efficient similarity matching across high-dimensional vector spaces.
The package wraps a highly optimized C++ implementation that uses a layered graph structure for sublinear search times. Unlike brute-force approaches that compare every vector, HNSW navigates through graph layers—sparse upper layers for coarse positioning and dense lower layers for precision—enabling searches across millions of vectors in milliseconds. The trade-off is approximate rather than exact results, which is acceptable for most machine learning applications where slight ranking variations don't impact user experience.
With over 56,000 weekly downloads, hnswlib-node has become the de facto choice for in-memory vector search in Node.js environments. It supports dynamic insertions, persistent storage via disk serialization, and configurable parameters for balancing speed versus accuracy. The library requires native compilation through node-gyp, meaning it's production-ready for server environments but may require build toolchain setup during development.
import { HierarchicalNSW } from 'hnswlib-node';
import fs from 'fs';
const dimension = 128;
const maxElements = 10000;
const indexPath = './vector-index.dat';
// Initialize or load existing index
const index = new HierarchicalNSW('l2', dimension);
if (fs.existsSync(indexPath)) {
index.readIndexSync(indexPath);
console.log(`Loaded index with ${index.getCurrentCount()} vectors`);
} else {
index.initIndex(maxElements, 16, 200, 100);
// M=16 connections per node, ef_construction=200, random_seed=100
}
// Add vectors with unique IDs
const sampleVectors = [
{ id: 1, vec: Array(dimension).fill(0).map(() => Math.random()) },
{ id: 2, vec: Array(dimension).fill(0).map(() => Math.random()) },
{ id: 3, vec: Array(dimension).fill(0).map(() => Math.random()) }
];
sampleVectors.forEach(({ id, vec }) => {
index.addPoint(vec, id);
});
// Search for 5 nearest neighbors
const query = Array(dimension).fill(0).map(() => Math.random());
index.setEf(50); // Higher ef = better accuracy, slower search
const result = index.searchKnn(query, 5);
console.log('Nearest neighbor IDs:', result.neighbors);
console.log('Distances:', result.distances);
// Persist to disk
index.writeIndexSync(indexPath);
console.log('Index saved successfully');Semantic search engines: Build text search systems that find documents by meaning rather than keywords. Embed documents and queries using sentence transformers, then use hnswlib-node to retrieve the most semantically similar content in real-time.
Image similarity and reverse image search: Store image embedding vectors (from models like ResNet or CLIP) and quickly find visually similar images. Ideal for photo management apps, e-commerce product matching, or content moderation systems.
Recommendation systems: Power collaborative filtering by storing user or item embeddings and finding nearest neighbors to suggest products, content, or connections. Handles millions of items with sub-100ms query times.
Real-time fraud detection: Maintain embeddings of transaction patterns and flag suspicious activity by finding nearest neighbors to known fraud signatures, enabling immediate risk assessment without database scans.
RAG (Retrieval-Augmented Generation) pipelines: Store document chunk embeddings for LLM context retrieval. When users query a chatbot, rapidly fetch relevant context passages to inject into prompts, forming the retrieval layer for AI applications.
npm install hnswlib-nodepnpm add hnswlib-nodebun add hnswlib-node