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; } }