My lightweight, Node-specific, no dependancy, simplified, cross-platform methods for manipulating files and folders syncronously with Node.js.
npm install myfs --save
Load myfs (choose one!)
// Load myfs for CommonJS:
//var myfs = require("myfs");
// Load myfs for ESM (native import):
import myfs from "myfs";
Here are a few examples of what you can do. There are many more features and examples than are shown in this readme. Please refer to the full documentation here.
Open / Read
var myfile = myfs.open("/path/to/folder/file1.txt"); // Yeilds the text contents of the file.
Save / Write
var myfile = myfs.save("/path/to/folder/file1.txt", data); // Wites the text data to the file.
Listing only files of X extension (NOTE: This can list recursively too.)
var mylist = myfs.listExt("/path/to/folder", "txt");
//
// Yeilds
// [
// "/path/to/folder/file1.txt",
// "/path/to/folder/file2.txt",
// "/path/to/folder/file3.txt"
// ]
Full documentation for all properties and methods available here: https://documon.net/projects/myfs/
You can also find the full documentation in the downloaded package locally (within node_modules) at:
/node_modules/myfs/docs
You can also download the docs (and source) from the repo here
Below is a quick reference for common methods.
This quick reference only includes the most commonly used methods, with simple descriptions.
Property | Description |
---|---|
slash | The kind of seperator used in paths. Windows = \ or POSIX = / |
__dirname | Same as native __dirname, but provices access for ESM imports. |
__filename | Same as native __filename, but provices access for ESM imports. |
Method | Arguments | Description |
---|---|---|
copy | src, dest | Copies a file or a folder from one location to another. Automatically handles creating destination folder structure if not exist. |
empty | path, dryRun | Recursively empties a folder of all it's contents (and all the sub-folder's contents), but leaves the source folder. |
list | from, filter, recursive, store | Read a folder and returns an object containing a "files" array and a "dirs" array. Each array lists full system paths. |
listExt | from, exts, recursive | Returns an array of paths for files that have the extension(s). The exts argument can be a comma-delimited string list of extensions. |
mkdir | path | Creates a folder at the specified location. The sub-folder heirarchy is constructed as needed. |
rm | src | Deletes a file from the system. |
rmdir | who, dryRun | Recursively removes a folder and all of it's sub-folders as well. |
move | src | Moves a file or folder. Can also be used to rename files and folders |
open | src, binary | Reads the text or binary data out of a file. |
save | src, data, binary | Saves data to a file. Overwrites entire file with provided data. |
isFile | src | Checks to see if src is a file, as opposed to exists, which checks if either file OR folder exists. |
isBinary | src | A cheap/fast check to see if a file's extension is in a list of known binary extensions. |
exists | path | Checks to see if a file or folder exists. |
isDir | src | Checks to see if src is a folder, as opposed to exists, which checks if either file OR folder exists. |
touch | src | Creates or updates the timestamp on a specific file or folder. |
dupe | src | Duplicates a file "in place" by making a copy with appended "copy N" in the file name. |
launch | target, opts | A file launcher. Opens stuff like websites, files, executables using the native system program. |
addSlash | path | Adds a trailing slash from path (if doesn't exist). |
removeSlash | path | Removes a trailing slash from path (if exists). |
resolve | path... | Generates an absolute path based on the provided arguments. |
basename | path, ext | Returns the last portion of a path, with or without extension. |
name | path | Returns the naked file name without extension. ("/foo/bar/bob.txt" --> "bob" |
ext | src | Returns the bare, base extension (no dot). |
clean | arg | Normalizes slashes by converting double \ to single \ and / to \ or \ tp / based on the current platform requirements. |
parent | path | Returns the path to the parent folder that the item resides within. |
join | paths... | Joins path segments and resolves relativity. |
normalize | path | Resolves ".." and "." portions of a path. Reduces double slashes to single (e.g. // -> / ). Forces back-slashes to forward slashes (e.g. \ -> / ). |
parse | path | Extracts basic path and file parts. root, dir, base, ext, name, ext2, extension, basename, filename, parent. Extends NodeJS's native "path.parse()" with additional fields with more context. |
cwd | tack | Gets the current working directory. Resolves the argument to the path. Simliar to native __dirname, which is depreciated. |
swapExt | src | Changes the path's (or filename's) extension. |
... | And many more! | Plus there are aliases for many methods. For those who think diffrently. |