Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Node.js, PHP and APIs

Node.js says it cannot find 'node:fs' after deploying to a server

ErrorError: 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.

  • Applies to Node.js 22, Node.js 24, Ubuntu 24.04
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 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 node
  2. 2Match what you see, then jump to its fix

    1. 1node -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. 2node -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. 3The 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

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 -v

2Fix

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 22

3Verify

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

Did this fix it?

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.js

3Verify

Restart the app and confirm it no longer throws the error.

nvm exec 22 node -v

Confirmed by github.com

Did this fix it?

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.js

Confirmed by nodejs.org

Did this fix it?

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

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.