naptha/tesseract.js · error
initialization failed
Error message
initialization failed
What it means
api.Init(null, langs, oem, configFile) returns -1 when Tesseract cannot initialize with the provided language data and engine mode. The library already attempts one automatic recovery: if cached data triggered the 'components are not present' debug message, it refreshes the cache and retries Init once. If the status is still -1 after that, it rejects with 'initialization failed'.
Source
Thrown at src/worker-script/index.js:305
const debugStr = TessModule.FS.readFile('/debugDev.txt', { encoding: 'utf8', flags: 'a+' });
if (dataFromCache && /components are not present/.test(debugStr)) {
log('Data from cache missing requested OEM model. Attempting to refresh cache with new language data.');
// In this case, language data is re-loaded
await loadLanguage({ workerId, payload: { langs: loadLanguageLangsWorker, options: loadLanguageOptionsWorker } }); // eslint-disable-line max-len
status = api.Init(null, langs, oem, configFile);
if (status === -1) {
log('Language data refresh failed.');
const delCachePromise2 = langsArr.map((lang) => adapter.deleteCache(`${loadLanguageOptionsWorker.cachePath || '.'}/${lang}.traineddata`));
await Promise.all(delCachePromise2);
} else {
log('Language data refresh successful.');
}
}
}
}
if (status === -1) {
res.reject('initialization failed');
}
res.progress({
workerId, status: statusText, progress: 1,
});
res.resolve();
} catch (err) {
res.reject(err.toString());
}
};
// Combines default output with user-specified options and
// counts (1) total output formats requested and (2) outputs that require OCR
const processOutput = (output) => {
const workingOutput = JSON.parse(JSON.stringify(defaultOutput));
const nonRecOutputs = ['imageColor', 'imageGrey', 'imageBinary', 'layoutBlocks', 'debug'];
let recOutputCount = 0;View on GitHub (pinned to a1ca80d9e3)
Solutions
- Clear the cache (delete the cachePath directory or set cacheMethod: 'refresh' for one run) to force a clean re-download.
- Match OEM to the data variant — legacy engine needs the full traineddata, LSTM needs the LSTM build; pass legacyCore/legacyLang consistently.
- Verify the lang string format ('+'-separated ISO 639-3 codes) and that every code was actually loaded.
- If passing a config object/string, simplify or remove it to isolate the cause.
Example fix
// before
const worker = await createWorker('eng', OEM.TESSERACT_ONLY); // LSTM data cached -> init -1
// after
const worker = await createWorker('eng', OEM.TESSERACT_ONLY, {
legacyCore: true,
legacyLang: true,
cacheMethod: 'refresh', // force re-download of correct data
}); Defensive patterns
Strategy: fallback
Validate before calling
// Ensure OEM and data variant are consistent before init.
const OEM = require('tesseract.js').OEM;
function consistentOemData(oem, legacyLang, legacyCore) {
const needsLegacy = oem === OEM.TESSERACT_ONLY || oem === OEM.TESSERACT_LSTM_COMBINED;
if (needsLegacy && (!legacyLang || !legacyCore)) {
throw new Error('Legacy OEM requested without legacyLang/legacyCore');
}
}
consistentOemData(OEM.TESSERACT_ONLY, options.legacyLang, options.legacyCore); Type guard
const oemRequiresLegacyData = (oem) => oem === 0 /* TESSERACT_ONLY */ || oem === 2 /* TESSERACT_LSTM_COMBINED */;
Try / catch
// On init failure, clear the cache once and retry with refreshed data.
async function initOrRefresh(langs, oem, opts) {
try {
return await createWorker(langs, oem, opts);
} catch (e) {
if (/initialization failed/i.test(e.message)) {
return await createWorker(langs, oem, { ...opts, cacheMethod: 'refresh' });
}
throw e;
}
} Prevention
- Keep OEM, legacyCore, and legacyLang aligned at worker creation; never mix an LSTM-only data cache with a legacy OEM.
- Clear the cache directory when upgrading tesseract.js or switching language data variants.
- Use cacheMethod: 'refresh' after upgrades to avoid stale corrupted downloads.
- Validate any custom config string/object against Tesseract's expected syntax before passing it.
When it happens
Trigger: Corrupted or truncated .traineddata; OEM mismatch such as requesting the legacy engine against LSTM-only language data; invalid/malformed lang string; partial download cached from a previous run; config file syntax error.
Common situations: Stale bad data in the cache directory from an interrupted download; using OEM.TESSERACT_ONLY with the default LSTM-only traineddata; custom config string that Tesseract rejects; disk/memory exhaustion during init.
Related errors
- Legacy model requested but code missing.
- `worker.detect` requires Legacy model, which was not loaded.
- Not found in cache
- Network error while fetching ${fetchUrl}. Response code: ${r
- Failed to load TesseractCore
AI-assisted analysis of naptha/tesseract.js@a1ca80d9e3 (2026-08-13).
Data as JSON: /api/errors/01bdc160dc23edc1.
Report an issue: GitHub.