Node.js says it cannot find 'node:fs' after deploying to a server
Error: Cannot find module 'node:fs'Short answer
The node: prefix is only understood by Node.js 14.18 or newer, and 16.0 or newer for require. On an older Node.js the name is treated as an ordinary package and not found. Print the version the server process really uses. Upgrade it, or make sure the service runs the right binary. Dropping the prefix also works.
Fix it fast
1Find out which cause you have
Print the Node.js version on the server, from the same shell or service account that starts the app.
node -v which node2Match what you see, then jump to its fix
- 1
node -v prints a version below 14.18 (or below 16 for require)The server has a Node.js release older than the prefix supportFix - 2
node -v in your terminal is new, but the app, PM2 or a service still failsThe service starts a different Node.js than your login shellFix - 3
The server is locked to an old Node.js, and the code or a dependency uses the node: prefixYou cannot upgrade yet, so remove the prefixFix
- 1
What does the error look like?
Error: Cannot find module 'node:fs'Require stack:
Cause 1 of 3
The server has a Node.js release older than the prefix support
1Check
Support for node: in require() was added in v14.18.0 and v16.0.0. For import in ES modules it arrived in v14.13.1 and v12.20.0. Anything older cannot resolve the prefixed name, and Node.js release lines that old are end of life.
node -v2Fix
Install a current LTS release. With nvm, install the latest LTS (Node.js 22 is one) and switch the current shell to it.
nvm install --lts
nvm use 223Verify
The version is now a supported LTS, and the app starts.
node -v
node -e "require('node:fs'); console.log('ok')"Confirmed by nodejs.org
Cause 2 of 3
The service starts a different Node.js than your login shell
1Check
nvm is loaded from your shell profile. In a non-interactive shell no profile files are read, so a script, cron job or service can pick up an older system Node.js, or none. Compare what that context sees.
bash -c 'which node; node -v'
bash -c 'source "$NVM_DIR/nvm.sh" && node -v'2Fix
Load nvm explicitly in the script that starts the app, or run it with a version named: nvm exec runs a command under a chosen version without changing the shell.
source "$NVM_DIR/nvm.sh"
nvm exec 22 node server.js3Verify
Restart the app and confirm it no longer throws the error.
nvm exec 22 node -vConfirmed by github.com
Cause 3 of 3
You cannot upgrade yet, so remove the prefix
1Check
Outside the few modules that need the prefix (node:sqlite, node:test and similar), core modules load by their plain name, which every Node.js release supports. Find where the prefix is used in your own code.
grep -rn "node:" --include=*.js --include=*.cjs --include=*.mjs src/2Fix
Use the plain name in your own code. A dependency that itself uses the prefix needs an older version of that package, or a newer Node.js.
// before
const fs = require('node:fs');
// after
const fs = require('fs');3Verify
Start the app on the old server.
node server.jsConfirmed by nodejs.org
How do I stop it coming back?
- Pin the Node.js version in .nvmrc and in the deployment script, so the server and your laptop run the same one.
- Only deploy to a supported LTS release; check the previous-releases page before choosing a server image.
- Print node -v at the start of the deploy log so a wrong version is obvious.
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
Node.js says Cannot find module when you run or require a file
Error: Cannot find module './routes/users'
Node.js looked for the name you gave it in every place its resolver knows and found nothing.
4 causes, each with a fixBack end
Node.js or the bundler cannot resolve fs/promises
Module not found: Error: Can't resolve 'fs/promises'
The fs/promises import path exists only in Node.js 14 and later, and only in code that runs on Node.js.
2 causes, each with a fixBack end
Application Load Balancer returns 502 for a Node.js target
HTTPCode_ELB_502_Count
An ALB returns 502 when the target gives it something it cannot use: a refused or reset connection, a connection closed mid-request, a bad response, or a header over 32 K.
4 causes, each with a fixBack end
corepack enable fails: permission denied while creating the pnpm link
Internal Error: EACCES: permission denied, symlink '../lib/node_modules/corepack/dist/pnpm.js' -> '/usr/local/bin/pnpm'
corepack enable writes small link files for yarn and pnpm next to your node binary.
2 causes, each with a fixBack end
Create React App says Node is too old, and npm will not run on the new one
npm does not support Node.js v10.19.0
The shell is running an old Node.js that is below the version Create React App needs.
3 causes, each with a fixBack end
firebase deploy cannot load your functions: user code failed to load
Functions codebase could not be analyzed successfully. It may have a syntax or runtime error
The Firebase CLI loads your functions code on your machine before it deploys, to learn which functions exist.
4 causes, each with a fix