nodejs保存文件,不存在则自动创建目录
const absolutePath = path.resolve(outputPath); const directoryPath = path.dirname(absolutePath); if (!fs.existsSync(directoryPath)) { try { fs.mkdirSync(directoryPath, { recursive: true }); console.log(`目录 ${directoryPath} 创建成功!`); } catch (err) { console.error(`创建目录 ${directoryPath} 失败: ${err}`); return; } }