Node.js or the bundler cannot resolve fs/promises
Error: Cannot find module 'fs/promises'Short answer
The fs/promises import path exists only in Node.js 14 and later, and only in code that runs on Node.js. Either your Node.js is older than 14, or a bundler is compiling Node-only code into a browser build. Upgrade Node.js, or keep the fs code out of the browser bundle.
Fix it fast
1Find out which cause you have
Check which Node.js version actually runs the command. It is often not the one you expect.
node --version which node2Match what you see, then jump to its fix
What does the error look like?
Error: Cannot find module 'fs/promises'Module not found: Error: Can't resolve 'fs/promises'
Cause 1 of 2
Node.js is older than 14
1Check
The fs/promises path was added in Node.js 14.0.0. Before that, the same API lived only at require('fs').promises.
node -e "console.log(process.version)"2Fix
Install a current LTS release of Node.js. If you cannot upgrade yet, read the API from fs instead.
// works on old and new Node.js
const { readFile } = require('fs').promises;
// current form
const { readFile: read } = require('node:fs/promises');3Verify
Run the script again.
node app.jsConfirmed by nodejs.org
Cause 2 of 2
Node-only code is being bundled for the browser
1Check
A browser build has no fs module. Webpack 5 no longer supplies stand-ins for Node core modules, so an import that reaches a client bundle fails to resolve. Find which file imports it.
grep -rn "fs/promises" src/2Fix
Move the file reading to server code (an API route, a build script or SSR-only module) and send the data to the browser. If a dependency imports it but never calls it in the browser, tell webpack to leave it out.
// webpack.config.js
module.exports = {
resolve: {
fallback: { 'fs/promises': false },
},
};3Verify
Rebuild; the unresolved-module error is gone.
npm run buildConfirmed by webpack.js.org
How do I stop it coming back?
- Pin the Node.js version in the project so every machine and CI runner uses the same one.
- Import Node built-ins with the node: prefix so it is obvious which code is server-only.
- Keep file-system code in modules that browser code never imports.
Sources
How was this page made?
I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.
Which stack is behind this page?
Projects of mine built on the same technologies:
Related fixes
Back end
ng serve says it cannot determine the project or target
Cannot determine project for command.
The Angular CLI looked through angular.json for a project that defines the target you asked for (serve, build, test) and found none, or found several and could not pick one.
3 causes, each with a fixBack end
ng fails with Cannot find module @angular/cli/bin/ng
Cannot find module '@angular/cli/bin/ng'
The ng command is the entry point to the Angular CLI package.
2 causes, each with a fixBack end
Angular app shows 'Cannot GET /' when served by a Node.js server
Cannot GET /dashboard
Cannot GET / is Express saying no route or file matches the request.
3 causes, each with a fixBack end
ng serve cannot find the dev-server builder package
Could not find the implementation for builder @angular/build:dev-server
The builder named in angular.json (for example @angular-devkit/build-angular:dev-server) is looked up as an npm package starting from the workspace root.
3 causes, each with a fixBack end
Angular build says it cannot resolve '@angular/localize/init'
Can't resolve '@angular/localize/init' in '/usr/src/app/src'
The build imports @angular/localize, and that package is not in node_modules.
2 causes, each with a fixBack end
The command npm run build -- --prod exited with code 1 (Angular)
The command "npm run build -- --prod" exited with code 1
Exit code 1 only says the build failed; the real message is above it.
2 causes, each with a fix