Fix 'Cannot find package mem-fs' Import Error

September, 22nd 2024 1 min read

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.

bash
npm install mem-fs --save
# or
yarn add mem-fs

Avoid global installs unless required:

bash
npm install -g mem-fs   # not recommended

2. 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 install

Yarn:

bash
yarn cache clean
rm -rf node_modules
yarn install

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

6. Install a Compatible Version

bash
npm install mem-fs@1.1.3

7. Local vs Global Conflict

If installed globally but not locally:

bash
npm link mem-fs

Recommendation: 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

StepFix
1Install mem‑fs locally
2Use correct module system
3Reinstall node_modules
4Check Node/npm versions
5Install 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.