WordNet Database Files for Node.js
wordnet-db is a specialized Node.js package designed to provide the complete database files for WordNet 3.1 directly within an npm installation. Currently at version 3.1.14, it offers a robust solution for developers requiring offline access to the Princeton WordNet lexical database, which is particularly useful for natural language processing libraries like `wordpos` and `natural`. The package differentiates itself by pre-bundling the entire 34 MB uncompressed WordNet dictionary, eliminating the need for on-demand downloads or complex setup scripts during application runtime. Its release cadence is primarily reactive, addressing compatibility with newer Node.js versions, fixing installation-related issues, or updating to newer WordNet database versions (though it has been stable on WordNet 3.1 for a while). This ensures that applications can deploy with a guaranteed, self-contained WordNet data source, even in environments with restricted internet access.
Common errors
-
Error: Cannot find module 'tar'
cause An older version of `wordnet-db` was installed which had `tar` as a runtime dependency, and `tar` failed to install correctly.fixUpgrade to `wordnet-db@^3.1.13` to use the version where `tar` is no longer a dependency and dictionary files are directly bundled. -
WordNet data files appear corrupted or have incorrect line endings on Windows.
cause When using `wordnet-db@3.0.0` or cloning the repository directly, Git's `core.autocrlf` setting on Windows converted line endings in the plain text data files, corrupting them.fixFor version `3.0.0` specifically, ensure `git config core.autocrlf false` is set before cloning the repository. For later versions (3.1.x), this issue is largely mitigated by how files are packaged.
Warnings
- breaking The package name was changed from `WNdb` to `wordnet-db` to comply with npm naming rules. While old links might still resolve, all new installations and references should use `wordnet-db`.
- gotcha Older Node.js versions (specifically <0.6) are not supported due to the package's internal reliance on Node.js's built-in `zlib` module for handling compressed data.
- breaking The method of database file distribution and installation changed significantly. In versions prior to `3.1.12`, a `tar` dependency was used. From `3.1.12` onwards, `tar` was removed, and from `3.1.13`, the `.tar.gz` archive was removed from the npm package, with dictionary files included directly. This affects installation logic and potential `tar` related issues.
- gotcha On Windows systems, if you are working with WordNet 3.0.0 directly from a git clone, automatic CRLF (Carriage Return Line Feed) conversion can corrupt the data files. This was relevant when text dict files were included uncompressed.
- gotcha Installation issues were reported with `npm@5` due to postinstall script order. Version `3.1.6` introduced a fix by including `dict` files directly and disabling the postinstall script.
Install
-
npm install wordnet-db -
yarn add wordnet-db -
pnpm add wordnet-db
Imports
- wordnetDb
import wordnetDb from 'wordnet-db';
const wordnetDb = require('wordnet-db'); - dbPath
import { path as dbPath } from 'wordnet-db';const dbPath = require('wordnet-db').path; - dbVersion
const { version: dbVersion } = require('wordnet-db');const dbVersion = require('wordnet-db').version;
Quickstart
const wordnetDb = require('wordnet-db');
const fs = require('fs');
const path = require('path');
console.log('--- WordNet DB Information ---');
console.log(`Package version: ${wordnetDb.libVersion}`);
console.log(`WordNet data version: ${wordnetDb.version}`);
console.log(`Database files path: ${wordnetDb.path}`);
console.log(`Number of files: ${wordnetDb.files.length}`);
// Verify a common WordNet data file exists
const exampleFile = 'data.noun';
const fullPath = path.join(wordnetDb.path, exampleFile);
console.log(`\nChecking for existence of "${exampleFile}" at "${fullPath}"...`);
try {
fs.accessSync(fullPath, fs.constants.F_OK);
console.log(`SUCCESS: "${exampleFile}" found. WordNet database seems correctly installed.`);
// Optionally, read a few lines to confirm content
const content = fs.readFileSync(fullPath, 'utf8');
console.log(`First 3 lines of ${exampleFile}:\n${content.split('\n').slice(0, 3).join('\n')}`);
} catch (err) {
console.error(`ERROR: "${exampleFile}" not found or inaccessible. Installation may be incomplete or corrupted.`);
console.error(err.message);
}