Node.js中fs模块调用异常返回"I'm fs modules"问题求助
Hey there, let's work through this annoying fs module troll problem step by step—you’re not missing something obvious, this is just a tricky edge case!
First off, remember: Node.js’s fs is a built-in module—you never need to install it via npm, so commands like npm uninstall fs don’t affect the native version. The issue is likely that Node is loading a malicious local/global module instead of the built-in one. Let’s dig into fixes:
1. Check for Local Fake fs Files
Node prioritizes loading files named fs.js/fs.node in your project directory over the built-in module.
- Run this in your project root:
- Linux/macOS:
ls -la | grep fs - Windows:
dir | findstr fs
- Linux/macOS:
- If you see any
fs.js,fs.json, orfs.nodefiles, delete them immediately.
2. Verify Node’s Module Search Paths
Node looks for modules in specific directories—one of them might have the troll module.
- Run
node -e "console.log(module.paths)"to see all search paths. - Check each path for a
fsfolder (even empty ones) and delete it if found.
3. Audit Your Webpack Configuration
Since you’re using Webpack 4, it’s possible your config is redirecting fs to a troll module via aliases.
- Open your
webpack.config.jsand look for aresolve.aliassection like this:resolve: { alias: { 'fs': './some-troll-file.js' // This is the culprit! } } - Delete any
fsalias entry, then restart your dev server or rebuild.
4. Clear npm Cache and Global Modules
Even after uninstalling, cached or leftover global modules might cause issues:
- Run
npm cache clean --forceto wipe the npm cache. - Check your global node_modules folder:
- Linux/macOS:
/usr/local/lib/node_modules - Windows:
C:\Users\<YourUsername>\AppData\Roaming\npm\node_modules
- Linux/macOS:
- Delete any
fsfolder you find here.
5. Test in a Clean Environment
To rule out project-specific issues:
- Create a new empty folder:
mkdir clean-fs-test && cd clean-fs-test - Make a simple test file
test.js:const fs = require('fs'); // Write a test file to confirm native fs works fs.writeFileSync('./check-native.txt', 'If you see this, native fs is working!'); console.log('Check the check-native.txt file—if it exists, we’re good.'); - Run
node test.js. If the file is created, your native fs module is fine—go back to your project and recheck steps 1-4. If not, redownload Node.js 14.15.4 from the official site, do a full uninstall (delete all Node-related folders), and reinstall with antivirus/proxy temporarily disabled.
6. Check for Modified NODE_PATH
A misconfigured NODE_PATH environment variable can redirect module loading:
- Linux/macOS: Run
echo $NODE_PATH—if it has strange paths, clear it withunset NODE_PATHand restart your terminal. - Windows: Run
echo %NODE_PATH%—if set, go to System Properties > Environment Variables and delete theNODE_PATHentry.
内容的提问来源于stack exchange,提问作者Goyeur

