Fix 'Cannot find package mem-fs' Import Error
When Node.js cannot locate the mem‑fs package, it usually means the dependency is missing, incorrectly installed, or incompatible with your project setup. Here are the most reliable ways to fix it.
1. Install the Package (Local Install Recommended)
bash
npm install mem-fs --save
# or
yarn add mem-fsAvoid global installs unless required:
bash
npm install -g mem-fs # not recommended2. Check package.json
json
{
"dependencies": {
"mem-fs": "^1.0.0"
}
}If missing → reinstall.
3. Clear Cache & Reinstall Dependencies
bash
npm cache clean --force
rm -rf node_modules
npm installYarn:
bash
yarn cache clean
rm -rf node_modules
yarn install4. Fix Import / Require Mismatch
CommonJS:
js
const memFs = require('mem-fs');ESM:
js
import memFs from 'mem-fs';❗ Mismatched module systems often cause this error.
5. Verify Node.js Compatibility
bash
node -v
npm -v6. Install a Compatible Version
bash
npm install mem-fs@1.1.37. Local vs Global Conflict
If installed globally but not locally:
bash
npm link mem-fsRecommendation: always install locally.
8. Using mem‑fs with Yeoman? Use This Template
js
const memFs = require('mem-fs');
const editor = require('mem-fs-editor');
const store = memFs.create();
const fs = editor.create(store);Summary
| Step | Fix |
|---|---|
| 1 | Install mem‑fs locally |
| 2 | Use correct module system |
| 3 | Reinstall node_modules |
| 4 | Check Node/npm versions |
| 5 | Install compatible version |
Conclusion
The error is almost always caused by a missing install, module mismatch, or corrupted node_modules. Follow the steps above to resolve it quickly.