node-fs: Legacy FS Extension
node-fs is an extension library for Node.js's native `fs` module, originally designed for very early Node.js versions (specifically, requiring Node.js >=0.1.97). It aimed to provide functionalities that were not natively available in the `fs` module at the time, such as recursive directory creation (`fs.createDirectory`, `fs.mkdirSyncRecursive`) and recursive directory removal (`fs.remove`, `fs.rmdirSyncRecursive`), as well as recursive directory watching (`fs.watchTree`). The package's last update was in 2011, and it reached version 0.1.7. Given the significant advancements in Node.js's native `fs` module (e.g., recursive options for `fs.mkdir` and `fs.rm` are standard since Node.js v10.12.0 and v12.10.0 respectively), `node-fs` is now obsolete. Modern Node.js versions provide these features natively, making this package unnecessary and incompatible with current development practices. It has no ongoing maintenance or active development.
Common errors
-
TypeError: fs.createDirectory is not a function
cause Attempting to use `fs.createDirectory` (or other `node-fs` methods) after requiring the native Node.js 'fs' module, or after `node-fs` failed to load/patch 'fs'.fixEnsure you are requiring `node-fs` correctly (`const fs = require('node-fs');`). However, the real fix is to stop using this abandoned package and migrate to native Node.js `fs` methods like `fs.mkdir(path, { recursive: true })`. -
Error: Cannot find module 'node-fs'
cause The `node-fs` package is not installed in `node_modules` or Node.js cannot resolve the module path.fixInstall the package via `npm install node-fs`. However, it's strongly recommended NOT to use this abandoned package. Instead, refactor your code to use the native `fs` module. -
Error: EACCES: permission denied, mkdir 'some/path'
cause The Node.js process does not have sufficient permissions to create a directory or file at the specified path, which is a common operating system permission error.fixEnsure the Node.js process has write permissions to the target directory. On Linux/macOS, check file permissions with `ls -l` and use `chmod` if necessary. On Windows, verify folder security settings. This error is not specific to `node-fs` but common for any file system operation.
Warnings
- breaking This package is entirely incompatible with modern Node.js versions (v10+). Its functionalities are either natively implemented or have different, incompatible APIs in current Node.js releases. Attempting to use it will lead to runtime errors or unexpected behavior.
- breaking The package is abandoned, with its last commit in 2011. It receives no maintenance, security updates, or bug fixes, making it a significant security risk for any application still using it.
- deprecated Many functionalities provided by `node-fs`, such as recursive `mkdir` and `rmdir`, were eventually integrated into the native Node.js `fs` module with a different API signature and improved behavior. The `node-fs` API is effectively deprecated by native `fs` advancements.
- gotcha The `fs.watchTree` method provided by this package is an inefficient and unreliable pattern for file system watching. Modern Node.js provides `fs.watch` which is more performant and robust, though still has platform-specific caveats.
- gotcha This package explicitly extends the Node.js `fs` module in a way that can lead to conflicts or unexpected behavior if other libraries also try to extend `fs` or if Node.js updates introduce new `fs` methods with the same names.
Install
-
npm install node-fs -
yarn add node-fs -
pnpm add node-fs
Imports
- fs
import { fs } from 'node-fs';const fs = require('node-fs'); - createDirectory
const { createDirectory } = require('node-fs');const fs = require('node-fs'); fs.createDirectory('./path/to/dir', 0o777, (err) => { /* ... */ }); - remove
require('node-fs').remove('./path');const fs = require('node-fs'); fs.remove('./path/to/remove', (err) => { /* ... */ });
Quickstart
const fs = require('node-fs');
const path = require('path');
const dirToCreate = path.join(__dirname, 'test-dir-legacy/sub-dir');
const fileToCopy = path.join(__dirname, 'test-file.txt');
const copiedFile = path.join(__dirname, 'test-dir-legacy/copied-file.txt');
// Create a dummy file for copying
require('fs').writeFileSync(fileToCopy, 'Hello from the past!');
console.log('Attempting to create directory recursively with node-fs...');
fs.createDirectory(dirToCreate, 0o777, true, (err) => {
if (err) {
console.error('Error creating directory:', err);
return;
}
console.log(`Directory created: ${dirToCreate}`);
console.log('Attempting to copy file with node-fs...');
fs.copy(fileToCopy, copiedFile, (err) => {
if (err) {
console.error('Error copying file:', err);
return;
}
console.log(`File copied from ${fileToCopy} to ${copiedFile}`);
// Clean up (using modern fs for robust cleanup)
console.log('Cleaning up...');
require('fs').unlinkSync(fileToCopy);
require('fs').unlinkSync(copiedFile);
require('fs').rmdirSync(path.dirname(dirToCreate), { recursive: true });
console.log('Cleanup complete.');
});
});