Skip to content

Emscripten dual-environment output (-sENVIRONMENT=web,node) breaks Next.js Turbopack client builds due to Node built-ins require("fs") / require("path") in generated glue #26134

Closed

Description

@softmarshmallow

When compiling with Emscripten for both web and Node (e.g. -sENVIRONMENT=web,node), the generated JS glue contains a Node-only branch that uses require("fs") and require("path") (and sometimes require("crypto")).

Modern web bundlers that do static analysis of imported modules (notably Next.js 16 Turbopack) may attempt to resolve these Node built-ins even when the Node branch is never executed in the browser. This causes the client build to fail.

Webpack can be configured with fallbacks, but Turbopack users may prefer not to introduce webpack-only config or disable Turbopack.

Environment

  • Emscripten SDK (emsdk): 4.0.14 (eff90ca04a3785f571a8095b3a42b63799cf384a)
  • Emscripten (emcc): 4.0.7 (8dc91db45bf96c174531006839472a3924d105aa)
  • Next.js: 16.1.3
  • Bundler: Turbopack (Next.js default in dev/build)
  • Node.js: 22.x (we used Node v22.15.0)
  • Target: -sENVIRONMENT=web,node + -sMODULARIZE=1 (common for packaging)

Steps to reproduce

  1. Build any Emscripten module with a dual environment:
emcc ... \
  -sENVIRONMENT=web,node \
  -sMODULARIZE=1 \
  -sEXPORT_NAME=createModule
  1. Publish/consume the generated module from a Next.js app and import it from a Client Component (or any code that ends up in the browser bundle).

  2. Run: (non-webpack bundlers)

next dev
# or
next build

Actual behavior

Turbopack fails the browser/client build with an error similar to:

Module not found: Can't resolve 'fs'
  .../dist/index.js:XX:YY
    var fs = require("fs");

Import traces typically show the Emscripten-generated module being pulled into the client component graph.

In our case, the generated bundle contains Node-only branches like:

if (ENVIRONMENT_IS_NODE) {
  var fs = require("fs");
  var nodePath = require("path");
}

Expected behavior

If the consuming application only needs the web path on the client, the existence of a Node-only branch in the generated glue should not cause a hard build-time failure.

In practice: importing a dual-environment Emscripten bundle should be safe in a browser bundle as long as the Node-only branch is not executed.

Notes / analysis

  • Even though code like:
if (ENVIRONMENT_IS_NODE) {
  var fs = require("fs");
}

is runtime-safe, some bundlers do not fully dead-code-eliminate or they still attempt to resolve require("fs") during analysis of the module. For Turbopack, this is currently a build error in client bundles.

  • Webpack users can often fix this via config (resolve.fallback.fs = false, etc.). Turbopack users may not want to depend on webpack config, or may not have an equivalent escape hatch for every scenario.

Proposed fix (generator-side): use node: specifiers for Node built-ins

Generate Node-only imports using explicit Node built-in specifiers:

  • require("node:fs") instead of require("fs")
  • require("node:path") instead of require("path")
  • require("node:crypto") instead of require("crypto")

Rationale:

  • node: is the modern, unambiguous form for Node built-ins.
  • It avoids accidental resolution to userland packages named fs, path, etc.
  • It gives bundlers a clearer signal that these are Node built-ins and may help them apply correct handling (e.g. mark as server-only or treat as builtin/external) during static analysis.

Alternatives (if node: alone is not sufficient)

If some bundlers still error merely by seeing a Node builtin specifier, a more robust generator-side approach is to avoid static requires in browser-consumed bundles, e.g.:

  • Use an indirection to avoid static resolution:
var nodeRequire = (0, Function("return require"))();
var fs = nodeRequire("node:fs");
  • Or isolate Node loader logic into a separate entry that is only imported in Node contexts.

However, the simplest “modern” improvement is to emit node: specifiers for built-ins as the default.

Why this matters

Dual-environment output is important for packages that want:

  • browser usage (WebAssembly via fetch)
  • Node usage (SSR, tooling, tests, or Node-side WASM loading)

Today, generating Node-only branches with require("fs") / require("path") makes it hard to ship a single package that works cleanly across both environments in modern Next/Turbopack setups without bundler-specific configuration.

Activity

  1. sbc100 commented on Jan 20, 2026

    @sbc100
    Collaborator

    I believe this may have already been addressed in #26079 which landed a few days ago. It added node: prefixes to these require statement.

  2. softmarshmallow commented on Jan 21, 2026

    @softmarshmallow
    Author

    seems promising. (havent tested) when can we expect that to be released ?
    is it going to be in 4.0.24

    while using emsdk, is there no option to use unreleased version of it?

  3. sbc100 commented on Jan 21, 2026

    @sbc100
    Collaborator

    Yes it will be in the next release.

    If you don't want to wait for a release you can try it right away with emsdk install tot (for Tip-Of-Tree)

  4. softmarshmallow commented on Jan 26, 2026

    @softmarshmallow
    Author

    fixed in 5.0.0, verified.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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