跳到主要内容

createDirIfNotExists

TS NODE

Creates a directory, if it does not exist.

Use fs.exists() to check if the directory exists, fs.mkdir() to create it. Checkout createDirIfNotExistsSync for synchronous API.

typescript
const { mkdir, exists } = require("fs");
const { promisify } = require("util");
const mkdirP = promisify(mkdir);
const existsP = promisify(exists);

const createDirIfNotExists = async (dir: string) =>
!(await existsP(dir)) ? await mkdirP(dir) : undefined;
typescript
(async function () {
console.log(await createDirIfNotExists("test")); // creates the directory 'test', if it doesn't exist
})();