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.
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.
Just change:
import fs from 'node:fs';
To:
import fs from 'fs';
This is backward-compatible with all supported versions of Node.js.
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
If you’re deploying to varied environments or shared servers, use ‘fs’ instead of ‘node:fs’ to ensure compatibility.
Work with our skilled Node developers to accelerate your project and boost its performance.
Hire Node.js Developers