Skip to content

Next.js: node-tfhe WASM loading fails with standalone output #374

@gustavovalverde

Description

@gustavovalverde

Summary

When using @zama-fhe/relayer-sdk in a Next.js application with output: "standalone", the node-tfhe dependency fails to load its WASM file at runtime.

Environment

  • @zama-fhe/relayer-sdk: 0.3.0-6
  • node-tfhe: 1.4.0-alpha.3 (transitive dependency)
  • Next.js: 16.0.10
  • Turbopack (Next.js 16 default bundler)

Error

ENOENT: no such file or directory, open '/ROOT/node_modules/node-tfhe/tfhe_bg.wasm'

Root Cause

node-tfhe/tfhe.js uses __dirname to locate its WASM file:

const wasmPath = `${__dirname}/tfhe_bg.wasm`;
const wasmBytes = require('fs').readFileSync(wasmPath);

When Next.js bundles this with output: "standalone", __dirname is resolved at build time to a placeholder path (/ROOT/...) instead of the actual runtime path.

Workaround

Add node-tfhe to serverExternalPackages in next.config.ts:

const nextConfig: NextConfig = {
  output: "standalone",
  serverExternalPackages: [
    "node-tfhe",
  ],
};

This prevents Next.js from bundling the package, allowing __dirname to resolve correctly at runtime.

Suggestion

Consider documenting this workaround for Next.js users, or exploring alternative WASM loading strategies that work with bundlers (e.g., using import.meta.url or async WASM imports).

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions