ensure-dir-tiny
v1.1.0
Published
π Checks if a folder exists; if not, it creates it. This prevents "File Not Found" errors when saving data.
Maintainers
Readme
ensure-dir
- π Lightweight package that ensures that the directory at the given path exists. Creates it recursively if it doesn't. Prevents
"File Not Found"errors when saving files. - β»οΈ Works seamlessly with
CommonJS,ESMandTypeScript
π¦ Install via NPM
$ npm i ensure-dir-tinyπ» Usage
- See examples below
CommonJS
const path = require('path');
const fs = require('fs');
const ensureDirTiny = require('ensure-dir-tiny');
const testDir = path.join(__dirname, 'demo/a/b/c');
console.log('Creating directory:', testDir);
ensureDirTiny(testDir);
if (fs.existsSync(testDir)) {
console.log('β
Directory exists β module works');
} else {
console.log('β Directory not created');
}
// --| Run again to test idempotency
ensureDirTiny(testDir);
console.log('β
Called twice without crashing');
ESM
import path from 'path';
import fs from 'fs';
import ensureDirTiny from 'ensure-dir-tiny';
import { fileURLToPath } from 'url';
// __dirname replacement in ESM
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const testDir = path.join(__dirname, 'demo/a/b/c');
console.log('Creating directory:', testDir);
ensureDirTiny(testDir);
if (fs.existsSync(testDir)) {
console.log('β
Directory exists β module works');
} else {
console.log('β Directory not created');
}
// --| Run again to test idempotency
ensureDirTiny(testDir);
console.log('β
Called twice without crashing');TypeScript
import path from 'path';
import fs from 'fs';
import ensureDirTiny from 'ensure-dir-tiny';
const testDir: string = path.join(__dirname, 'demo/a/b/c');
console.log('Creating directory:', testDir);
ensureDirTiny(testDir);
if (fs.existsSync(testDir)) {
console.log('β
Directory exists β module works');
} else {
console.log('β Directory not created');
}
// --| Run again to test idempotency
ensureDirTiny(testDir);
console.log('β
Called twice without crashing');