
Security News
n8n Tops 2025 JavaScript Rising Stars as Workflow Platforms Gain Momentum
n8n led JavaScript Rising Stars 2025 by a wide margin, with workflow platforms seeing the largest growth across categories.
@mojojs/path
Advanced tools
A convenient little wrapper around fs and friends. Providing a container class for file system paths with a friendly API for dealing with different operating systems. Written in TypeScript.
import Path from '@mojojs/path';
// Current working directory (portable)
const dir = new Path();
// Relative file path "test.txt" (portable)
const file = new Path('test.txt');
// Relative file path "files/test.txt" (portable, POSIX example)
const file = new Path('files', 'test.txt');
// Relative file path "files/test.txt" (not portable)
const file = new Path('files/test.txt');
// Absolute file path "/home/kraih/test.txt" (not portable)
const file = new Path('/home/kraih/test.txt');
// Current file (portable)
const file = Path.currentFile();
// Caller file (portable)
const file = Path.callerFile();
Paths will be automatically split and joined with the correct separator for the current operating system. For the following examples we will be using POSIX paths and assume a Linux operating system.
// "/home/kraih/files/test.txt"
new Path('/home/kraih').child('files', 'test.txt');
// "/home/kraih/files/hello.txt"
new Path('/home/kraih/files/test.txt').sibling('hello.txt');
// "test.txt"
new Path('/home/kraih/test.txt').basename();
// "foo/bar"
new Path('/home/kraih/test.txt').dirname();
// ".txt"
new Path('/home/kraih/test.txt').extname();
// ".json"
new Path('/home/kraih/files/test.txt').sibling('hello.json').extname();
// "file:///home/kraih/test.txt"
new Path('/home/kraih/test.txt').toFileURL().toString();
// "['files', 'test.txt']"
new Path('files/test.txt').toArray();
// Caller directory
Path.callerFile().dirname();
Almost all methods will return this or a new instance of Path, depending on what makes most sense.
// Write file (async)
const file = await new Path('/home/kraih/test.txt').writeFile('Hello World!');
// Write file (sync)
const file = new Path('/home/kraih/test.txt').writeFileSync('Hello World!');
// Read file (async)
const content = await new Path('/home/kraih/test.txt').readFile('utf8');
// Read file (sync)
const content = new Path('/home/kraih/test.txt').readFileSync('utf8');
// Create file or update utime
await new Path('/home/kraih/test.txt').touch();
new Path('/home/kraih/test.txt').touchSync();
// Open file (async)
const fh = await new Path('/home/kraih').child('test.txt').open('w');
await fh.write('Hello ');
await fh.write('JavaScript!');
await fh.close();
// Create writable stream
const writable = new Path('test.txt').createWriteStream({encoding: 'utf8'});
// Create readable stream
const readable = new Path('test.txt').createReadStream({encoding: 'utf8'});
// Read lines from file
for await (const line of new Path('test.txt').lines({encoding: 'UTF-8'})) {
console.log(line);
}
Temporary directories will be deleted automatically when node stops, but can also be removed manually with the destroy
method. They are created relative to the operating system temp directory with node- prefix.
// Create a temporary directory (async)
const dir = await Path.tempDir();
await dir.child('test.txt').touch();
await dir.destroy();
// Create a temporary directory (sync)
const dir = Path.tempDirSync();
dir.child('test.txt').touchSync();
dir.destroySync();
There is a *Sync alternative for almost every method returning a Promise. All fs.constants are available via
Path.constants.
// Make file read-only
const file = await new Path('test.txt').chmod(Path.constants.O_RDONLY);
const file = new Path('test.txt').chmodSync(Path.constants.O_RDONLY);
// Check file access (async)
const isReadable = await new Path('test.txt').access(Path.constants.R_OK);
const isReadable = await new Path('test.txt').isReadable();
const isWritable = await new Path('test.txt').isWritable();
// Check file access (sync)
const isReadable = new Path('test.txt').accessSync(Path.constants.R_OK);
const isReadable = new Path('test.txt').isReadableSync();
const isWritable = new Path('test.txt').isWritableSync();
// Check if file exists
const exists = await new Path('test.txt').exists();
const exists = new Path('test.txt').existsSync();
// Check if file is absolute
const isAbsolute = new Path('test.txt').isAbsolute();
// Resolve path on file system
const real = await new Path('test.txt').realpath();
const real = new Path('test.txt').realpathSync();
// Create directory
const dir = await new Path('test', '123').mkdir({recursive: true});
const dir = new Path('test', '123').mkdirSync({recursive: true});
// Remove directory
await new Path('test').rm({recursive: true});
new Path('test').rmSync({recursive: true});
// List files in directory
for await (const file of new Path('test').list()) {
console.log(file.toString());
}
// List files recursively
for await (const file of new Path('test').list({recursive: true})) {
console.log(file.toString());
}
// Update atime and mtime
const file = await new Path('test.txt').utimes(new Date(), new Date());
const file = new Path('test.txt').utimesSync(new Date(), new Date());
// Symlink file
const file = await new Path('foo.txt').symlink(new Path('bar.txt'));
const file = new Path('foo.txt').symlinkSync(new Path('bar.txt'));
// Copy file
const file = await new Path('foo.txt').copyFile(new Path('bar.txt'));
const file = new Path('foo.txt').copyFileSync(new Path('bar.txt'));
// Rename file
await new Path('foo.txt').rename(new Path('bar.txt'));
new Path('foo.txt').renameSync(new Path('bar.txt'));
// Check is file is a directory (async)
const stat = await new Path('test').stat();
const isDirectory = stat.isDirectory();
const lstat = await new Path('test').lstat();
const isDirectory = lstat.isDirectory();
// Check is file is a directory (sync)
const isDirectory = new Path('test').statSync().isDirectory();
const isDirectory = new Path('test').lstatSync().isDirectory();
All you need is Node.js 16.0.0 (or newer).
$ npm install @mojojs/path
FAQs
Convenient container class for file system paths
The npm package @mojojs/path receives a total of 1,488 weekly downloads. As such, @mojojs/path popularity was classified as popular.
We found that @mojojs/path demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 4 open source maintainers collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Security News
n8n led JavaScript Rising Stars 2025 by a wide margin, with workflow platforms seeing the largest growth across categories.

Security News
The U.S. government is rolling back software supply chain mandates, shifting from mandatory SBOMs and attestations to a risk-based approach.

Security News
crates.io adds a Security tab backed by RustSec advisories and narrows trusted publishing paths to reduce common CI publishing risks.