Skip to content

Module not found: Can't resolve 'fs' in Next.js Client Component (Turbopack) #151

@mostafa1310

Description

@mostafa1310

I am encountering a build error when attempting to use harfbuzzjs within a Next.js application, specifically within a client-side component, while using next dev --turbopack. The build fails with a "Module not found: Can't resolve 'fs'" error, pointing to node_modules/harfbuzzjs/hb.js.

const hbPromise =
  typeof window !== "undefined"
    ? import("harfbuzzjs")
    : Promise.resolve({ default: {} });

let hb: any;
export async function initHarfBuzz() {
  if (!hb) hb = await hbPromise;
  return hb;
}

export async function shapeArabicText(
  fontArrayBuffer: ArrayBuffer,
  text: string,
) {
  const hbInst = await initHarfBuzz();
  const blob = hbInst.createBlob(fontArrayBuffer);
  const face = hbInst.createFace(blob, 0);
  const font = hbInst.createFont(face);
  font.setScale(1000, 1000);

  const buf = hbInst.createBuffer();
  buf.addText(text);
  buf.guessSegmentProperties();
  hbInst.shape(font, buf);
  const json = buf.json(); // contains glyph info + clusters + positions
  buf.destroy();
  font.destroy();
  face.destroy();
  blob.destroy();
  return json;
}

Error.txt

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