該EEXIST錯(cuò)誤是文件或目錄存在但嘗試的操作要求其不存在時(shí)遇到的另一個(gè)文件系統(tǒng)錯(cuò)誤。例如,當(dāng)您嘗試創(chuàng)建已存在的目錄時(shí),您將看到此錯(cuò)誤,如下所示:
const fs = require('fs'); fs.mkdirSync('temp', (err) => { if (err) throw err; });
獲得以下報(bào)錯(cuò)信息
文章來源:http://www.zghlxwxcb.cn/article/495.html
Error: EEXIST: file already exists, mkdir 'temp' at Object.mkdirSync (node:fs:1349:3) at Object.<anonymous> (/home/ayo/dev/demo/main.js:3:4) at Module._compile (node:internal/modules/cjs/loader:1099:14) at Object.Module._extensions..js (node:internal/modules/cjs/loader:1153:10) at Module.load (node:internal/modules/cjs/loader:975:32) at Function.Module._load (node:internal/modules/cjs/loader:822:12) at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:77:12) at node:internal/main/run_main_module:17:47 { errno: -17, syscall: 'mkdir', code: 'EEXIST', path: 'temp' }
解決方案
fs.existsSync() 這里的解決方案是在嘗試創(chuàng)建路徑之前檢查路徑是否存在:文章來源地址http://www.zghlxwxcb.cn/article/495.html
const fs = require('fs'); if (!fs.existsSync('temp')) { fs.mkdirSync('temp', (err) => { if (err) throw err; }); }
到此這篇關(guān)于Error: EEXIST: file already exists, mkdir temp的文章就介紹到這了,更多相關(guān)內(nèi)容可以在右上角搜索或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!