The error you’re encountering:

Cannot find module 'node:fs'

means that your code is using the Node.js “prefix” import style (node:fs), but your server environment does not support it, likely due to an older Node.js version.

What’s ‘node:fs’?

import fs from 'node:fs';

This is a modern, fully-qualified Node.js import path (introduced in Node.js v14.18.0+, fully supported in v16+). It’s functionally equivalent to:

import fs from 'fs';

But the node: prefix tells the module loader that this is a core module, not a package.

Fix Options

Option 1: Use legacy style (‘fs’ instead of ‘node:fs’)

Just change:

import fs from 'node:fs';

To:

import fs from 'fs';

This is backward-compatible with all supported versions of Node.js.

Option 2: Upgrade Node.js on Your Linux Server

If you want to use node:fs, you’ll need Node.js v14.18.0+, ideally v16 or v18.

To upgrade:

# Check version
node -v

# Use Node Version Manager (nvm) to install latest stable version
curl -o- raw.githubusercontent.com/nvm-sh/nvm/v0.39.7/install.sh | bash
source ~/.bashrc  # or ~/.zshrc

nvm install --lts
nvm use --lts

Then check again:

node -v  # Should be >= 16.x

Recommendation

If you’re deploying to varied environments or shared servers, use ‘fs’ instead of ‘node:fs’ to ensure compatibility.

Also Read

Also Read:

Node JS Multer

Need Help With Node Development?

Work with our skilled Node developers to accelerate your project and boost its performance.

Hire Node.js Developers

Support On Demand!

Related Q&A