Hunspell Spellchecker in Javascript
hunspell-spellchecker is a lightweight JavaScript library designed to parse and utilize Hunspell dictionaries (typically `.aff` and `.dic` files) within a JavaScript environment. It facilitates the conversion of these dictionary files into a JSON format, which can then be efficiently loaded and used for spell-checking. The library supports both Node.js for server-side processing and, with careful pre-processing, browser environments. The current stable version is 1.0.2, last published over 11 years ago, indicating it is no longer actively maintained. Its primary differentiator is the ability to work directly with Hunspell dictionary formats, converting them to a more JavaScript-friendly JSON structure for use in applications requiring custom or offline spell-checking capabilities.
Common errors
-
ReferenceError: require is not defined
cause Attempting to use `require()` in an ES Module (ESM) context without proper configuration or transpilation.fixEnsure your project is configured for CommonJS, or if using ESM, adjust your build setup to handle CommonJS imports. For strict ESM, rewrite the import statement using `import * as Spellchecker from 'hunspell-spellchecker';` (though this package is CJS-first, this might still lead to issues) or use dynamic `import('hunspell-spellchecker')`. -
TypeError: fs.readFileSync is not a function
cause Trying to run the dictionary parsing example code directly in a browser environment.fixThe `fs` module is a Node.js built-in. Dictionary parsing must occur on the server-side (Node.js) or as part of a build process. Only the resulting JSON dictionary should be loaded in the browser. -
Error: ENOENT: no such file or directory, open './en_EN.aff'
cause The specified Hunspell dictionary (`.aff` or `.dic`) file path is incorrect or the file does not exist at the given location.fixVerify the file paths provided to `fs.readFileSync()` are absolute or correctly relative to `process.cwd()` (Node.js) or `__dirname`. Ensure the dictionary files are present in the expected location.
Warnings
- breaking This package is abandoned and has not been updated in over 11 years. It is unlikely to receive security patches, bug fixes, or new features. Consider more actively maintained alternatives like `nspell` or `nuspell` for modern applications.
- gotcha The example code relies on Node.js's `fs.readFileSync` for parsing dictionary files. This makes the parsing step inherently server-side. Direct use in a browser environment will fail unless dictionary files are pre-parsed into JSON and loaded, or `fs` is polyfilled/mocked.
- gotcha The library is CommonJS-only and does not natively support ES Modules (ESM) `import` syntax. Attempting to use `import Spellchecker from 'hunspell-spellchecker'` in an ESM context will likely result in errors.
Install
-
npm install hunspell-spellchecker -
yarn add hunspell-spellchecker -
pnpm add hunspell-spellchecker
Imports
- Spellchecker
import Spellchecker from 'hunspell-spellchecker';
const Spellchecker = require('hunspell-spellchecker'); - Spellchecker (instance)
const spellchecker = hunspell_spellchecker();
const spellchecker = new Spellchecker();
Quickstart
const fs = require('fs');
const path = require('path');
const Spellchecker = require('hunspell-spellchecker');
// Initialize a spellchecker instance
const spellchecker = new Spellchecker();
// Example dictionary files (replace with actual paths)
// For demonstration, these files must exist in your project root
// or be accessible via `fs.readFileSync`.
// You would typically download official Hunspell dictionaries (e.g., from LibreOffice).
const affFilePath = path.join(__dirname, 'en_US.aff'); // Placeholder
const dicFilePath = path.join(__dirname, 'en_US.dic'); // Placeholder
// In a real application, ensure these files exist
// For testing, you might create dummy files or skip this part.
if (!fs.existsSync(affFilePath) || !fs.existsSync(dicFilePath)) {
console.error('Error: Dictionary files (en_US.aff, en_US.dic) not found.');
console.error('Please download them or provide valid paths for parsing.');
console.error('For example, download from: https://extensions.libreoffice.org/extensions/english-dictionaries (rename .oxt to .zip)');
process.exit(1);
}
// Parse an hunspell dictionary that can be serialized as JSON
const DICT = spellchecker.parse({
aff: fs.readFileSync(affFilePath),
dic: fs.readFileSync(dicFilePath)
});
// Load a serialized dictionary into the spellchecker
spellchecker.use(DICT);
// Check a word
const wordToCheck = 'typo';
const isRight = spellchecker.check(wordToCheck);
console.log(`Is '${wordToCheck}' spelled correctly? ${isRight}`); // Should be false
const correctWord = 'hello';
const isCorrect = spellchecker.check(correctWord);
console.log(`Is '${correctWord}' spelled correctly? ${isCorrect}`); // Should be true