{"record":{"id":"a3e90f6f6e04d8a9","repo":"huggingface/tokenizers","slug":"wasi-binding-not-found-and-napi-rs-force-wasi-is-s","errorCode":null,"errorMessage":"WASI binding not found and NAPI_RS_FORCE_WASI is set to error","messagePattern":"WASI binding not found and NAPI_RS_FORCE_WASI is set to error","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"bindings/node/index.js","lineNumber":722,"sourceCode":"        if (!wasiBindingError) {\n          wasiBindingError = err\n        } else {\n          wasiBindingError.cause = err\n        }\n        loadErrors.push(err)\n      }\n    }\n  }\n  if (process.env.NAPI_RS_FORCE_WASI === 'error' && !wasiBinding) {\n    const error = new Error('WASI binding not found and NAPI_RS_FORCE_WASI is set to error')\n    error.cause = wasiBindingError\n    throw error\n  }\n}\n\nif (!nativeBinding) {\n  if (loadErrors.length > 0) {\n    throw new Error(\n      `Cannot find native binding. ` +\n        `npm has a bug related to optional dependencies (https://github.com/npm/cli/issues/4828). ` +\n        'Please try `npm i` again after removing both package-lock.json and node_modules directory.',\n      {\n        cause: loadErrors.reduce((err, cur) => {\n          cur.cause = err\n          return cur\n        }),\n      },\n    )\n  }\n  throw new Error(`Failed to load native binding`)\n}\n\nmodule.exports = nativeBinding\nmodule.exports.AddedToken = nativeBinding.AddedToken\nmodule.exports.BPE = nativeBinding.BPE\nmodule.exports.Bpe = nativeBinding.Bpe","sourceCodeStart":704,"sourceCodeEnd":740,"githubUrl":"https://github.com/huggingface/tokenizers/blob/6cfd9d385ca0ed91c10b49f0ce97d02cfde1b607/bindings/node/index.js#L704-L740","documentation":"When NAPI_RS_FORCE_WASI is set, the loader refuses to use the plain native binding and insists on loading a WASI binding (./tokenizers.wasi.cjs or the tokenizers-wasm32-wasi package). If neither WASI binding can be required and the variable is set to the exact value 'error', the loader throws this error (wrapping the underlying require failure as `cause`). It exists so users can force WASI mode and hard-fail instead of silently falling back.","triggerScenarios":"process.env.NAPI_RS_FORCE_WASI === 'error' at require time and both require('./tokenizers.wasi.cjs') and require('tokenizers-wasm32-wasi') fail (files/packages absent or unloadable).","commonSituations":"Setting NAPI_RS_FORCE_WASI=error in an environment (e.g. serverless/WebAssembly targets) where the wasm32-wasi optional package was never installed; Docker images that strip optional dependencies; typos leaving the variable set to 'error' unintentionally.","solutions":["Install the WASI binding: `npm i tokenizers-wasm32-wasi` (ensure optional dependencies aren't skipped)","Remove NAPI_RS_FORCE_WASI (or set it to anything other than 'error', e.g. 'warn') so the loader can use the native binding","Check error.cause to see the underlying require failure (missing module, syntax error) and fix that"],"exampleFix":"// before\nNAPI_RS_FORCE_WASI=error node app.js\n// after\nnpm i tokenizers-wasm32-wasi\n# or\nunset NAPI_RS_FORCE_WASI","handlingStrategy":"validation","validationCode":"if (process.env.NAPI_RS_FORCE_WASI === 'error') {\n  let ok = false;\n  try { require('tokenizers-wasm32-wasi'); ok = true; } catch {}\n  if (!ok) console.error('NAPI_RS_FORCE_WASI=error but WASI binding is not installed');\n}","typeGuard":"function wasiBindingAvailable() {\n  try { require.resolve('tokenizers-wasm32-wasi'); return true; } catch { return false; }\n}","tryCatchPattern":"try {\n  const tokenizers = require('tokenizers');\n} catch (err) {\n  if (String(err.message).includes('NAPI_RS_FORCE_WASI')) {\n    console.error('WASI binding missing; install tokenizers-wasm32-wasi or unset NAPI_RS_FORCE_WASI');\n  }\n  throw err;\n}","preventionTips":["Only set NAPI_RS_FORCE_WASI=error when tokenizers-wasm32-wasi is a declared dependency","Use 'warn' instead of 'error' when you want a warning, not a hard failure","Include wasm32-wasi in CI images that run with the env var set","Inspect error.cause when it throws to find the real require failure"],"tags":["wasi","env-var","napi-rs","nodejs"],"backgroundTag":"missing-optional-dependency","analyzedSha":"6cfd9d385ca0ed91c10b49f0ce97d02cfde1b607","analyzedAt":"2026-09-09T11:43:25.027Z","contentChangedAt":"2026-09-09T11:43:25.027Z","schemaVersion":2},"datasetVersion":"2026-09-16T04:17:20.429Z"}