Repository navigation
Description
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
- Build any Emscripten module with a dual environment:
emcc ... \
-sENVIRONMENT=web,node \
-sMODULARIZE=1 \
-sEXPORT_NAME=createModule-
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).
-
Run: (non-webpack bundlers)
next dev
# or
next buildActual 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 ofrequire("fs")require("node:path")instead ofrequire("path")require("node:crypto")instead ofrequire("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
sbc100 commentedon Jan 20, 2026
I believe this may have already been addressed in #26079 which landed a few days ago. It added
node:prefixes to these require statement.softmarshmallow commentedon Jan 21, 2026
seems promising. (havent tested) when can we expect that to be released ?
is it going to be in
4.0.24while using emsdk, is there no option to use unreleased version of it?
sbc100 commentedon Jan 21, 2026
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)softmarshmallow commentedon Jan 26, 2026
fixed in 5.0.0, verified.
Patch satellite.js so it can be bundled for a browser