Module not found: Error: Can't resolve 'fs' halts StackBlitz bootstrap
0 reputation · 12 May 2022, 18:22 UTC
0 reputation · 12 May 2022, 18:22 UTC
When prototyping in StackBlitz, developers often add Node.js core modules (e.g., fs, path, crypto) to reuse server‑side logic. The goal is to instrument the application with timing calls to identify slow sections before refactoring.
Importing fs triggers a Module not found: Error: Can't resolve 'fs' during the webpack build. This error stops the application bootstrap, preventing any subsequent console.time or other instrumentation from executing. The console output shows the failure before the app renders, so a performance profiler may incorrectly report a slowdown where none exists.
StackBlitz documentation advises avoiding Node‑specific APIs in the browser environment but does not provide an automated warning or fallback mechanism. Developers must manually remove the import or configure a polyfill via resolve.fallback in a custom webconfig.js to continue execution.
Choosing between adding a polyfill (which increases bundle size and could introduce new bottlenecks) or extracting the logic into a separate service remains an open question for teams relying on fast prototyping.
What strategies can detect this build‑time error early during profiling? What configuration changes allow performance instrumentation to run while still using Node core modules? Will polyfilling fs introduce measurable overhead that could itself become a bottleneck?
34775 reputation · 13 May 2022, 00:10 UTC
Module not found: Error: Can't resolve 'fs' is a compile-time module-resolution failure, not a runtime performance problem. StackBlitz browser projects bundle with webpack 5, which no longer ships automatic polyfills for Node core modules. The build aborts before any of your code executes, so no console.time output exists to measure — anything your profiler reports around this failure describes the broken build, not your application.
Fastest unblocking fix: remove the fs import, or tell webpack the module is intentionally absent:
// webpack config override (e.g. via a custom config in your StackBlitz project)
module.exports = {
resolve: {
fallback: { fs: false }
}
};Then guard every code path that touches fs, because the bundler will now succeed but an unguarded call still throws at runtime:
const isNode = typeof process !== 'undefined' && process.versions?.node;
if (isNode) {
// Node-only logic here
}Confirmed, well-established behavior:
fs in a browser-targeted build is now a hard error by design.Likely, but verify for your project: StackBlitz runs two different engines. Browser-only projects have no Node runtime at all. WebContainers-based projects boot a real Node environment in the browser, where fs works in server-side code. Check whether your project starts a Node server; if it does, the right fix is moving the fs logic server-side rather than polyfilling anything.
from 'fs', require('path') in files under your browser entry point.Polyfills such as browserify-fs or memfs emulate the filesystem in memory or IndexedDB. Two cautions:
The cleaner long-term fix is isolating Node-dependent logic behind an interface and swapping implementations per environment (dependency injection or conditional exports), so the browser bundle never resolves fs at all. For pure prototyping, resolve.fallback: { fs: false } plus environment guards is the lowest-effort path that keeps instrumentation honest.
import fs from 'fs', confirm the build fails before app code runs.console.time now reports timings.One open detail that could change this recommendation: whether your StackBlitz project is WebContainers-based. If it is, share whether the fs usage can move into the server process — that would make polyfilling unnecessary entirely.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.