Error: ENOTDIR: not a directory, scandir ‘./src/functions/.DS_Store’ 这是什么,如何修复?

huangapple go评论102阅读模式
英文:

Error: ENOTDIR: not a directory, scandir './src/functions/.DS_Store' What is this and how to fix it

问题

The error message you provided appears to be related to a directory scanning issue in your code. Here's the translated part:

错误: ENOTDIR: 不是目录, scandir './src/functions/.DS_Store'

And the translated code snippet:

给出错误的代码部分:

const functionFolders = fs.readdirSync(`./src/functions`);
for (const folder of functionFolders) {
  const functionFiles = fs
    .readdirSync(`./src/functions/${folder}`)
    .filter((file) => file.endsWith(".js"));
  for (const file of functionFiles) require(`./functions/${folder}/${file}`);
}

It seems you encountered an issue while working on your Discord bot's Logs function. If you have any specific questions or need further assistance, please feel free to ask.

英文:
node:internal/fs/utils:351
    throw err;
    ^


Error: ENOTDIR: not a directory, scandir './src/functions/.DS_Store'
    at Object.readdirSync (node:fs:1532:3)
    at Object.<anonymous> (/Users/roopa/Desktop/projects/LLbot/src/bot.js:43:6)
    at Module._compile (node:internal/modules/cjs/loader:1255:14)
    at Module._extensions..js (node:internal/modules/cjs/loader:1309:10)
    at Module.load (node:internal/modules/cjs/loader:1113:32)
    at Module._load (node:internal/modules/cjs/loader:960:12)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:83:12)
    at node:internal/main/run_main_module:23:47 {
  errno: -20,
  syscall: 'scandir',
  code: 'ENOTDIR',
  path: './src/functions/.DS_Store'
}

part of code which is giving error

const functionFolders = fs.readdirSync(`./src/functions`);
for (const folder of functionFolders) {
  const functionFiles = fs
    .readdirSync(`./src/functions/${folder}`)
    .filter((file) => file.endsWith(".js"));
  for (const file of functionFiles) require(`./functions/${folder}/${file}`);
}

I was trying to make Logs function for my Discord bot and saw that nothing is happening and everything broke after writing code, so I removed all files and code from main 'bot.js' file and now it's giving this error

答案1

得分: 1

fs.readdirSync 返回一个目录的所有内容,包括文件和文件夹,而不仅仅是文件夹。.DS_Store 是 Mac 用于存储 Finder 信息的特殊文件(不是文件夹)。

在你贴出的示例代码中,请确保在迭代 for 循环中遍历内容之前检查 readdirSync() 返回的项目是否是目录,类似这样:

const functionFolders = fs.readdirSync(`./src/functions`).filter(item => item.isDirectory())
for (const folder of functionFolders) {
  // 其余逻辑
}
英文:

fs.readdirSync returns all the contents of a directory including files and not just folders. .DS_Store is a special file (not folder) used by Mac to store Finder information.

In the sample code you pasted, ensure to check whether the item returned by readdirSync() is a directory or not before iterating over the contents in the for loop, something like this

const functionFolders = fs.readdirSync(`./src/functions`).filter(item => item.isDirectory())
for (const folder of functionFolders) {
  // rest of the logic
}

答案2

得分: -1

好的,以下是翻译好的部分:

所以要删除 .DS_Store 文件,我只需在终端中输入以下命令,并且它有效。

另外,今天我遇到了关于 'fs.readdirSync()' 函数的另一个错误,说提供的目录找不到,所以为了修复这个问题,只需提供文件夹/文件的完整路径。 (例如,向下滚动)

在终端中输入的删除所有 .DS_Store 文件的命令行。

find . -name ".DS_Store" -type f -delete

示例:

(之前)

const commandsFolder = fs.readdirSync('./Commands');

(之后)

const commandsFolder = fs.readdirSync('/Users/roopa/Desktop/projects/LLbot/src/Commands');

希望对你有所帮助 Error: ENOTDIR: not a directory, scandir ‘./src/functions/.DS_Store’ 这是什么,如何修复?

对于糟糕的英语表示抱歉 :')

英文:

ok so to remove .DS_Store files I just wrote the following line in the terminal and it worked.

Also today I had another error regarding 'fs.readdirSync()' function, said that the provided directory cannot be found so to fix that just give the whole path to your folder/file. (For Example, Scroll downwards)

The line to write in the terminal to remove/delete all .DS_Store files.

> find . -name ".DS_Store" -type f -delete

Example :

(BEFORE)

const commandsFolder = fs.readdirSync('./Commands');

(AFTER)

const commandsFolder = fs.readdirSync('/Users/roopa/Desktop/projects/LLbot/src/Commands');

Hope it helps Error: ENOTDIR: not a directory, scandir ‘./src/functions/.DS_Store’ 这是什么,如何修复?

sorry for bad english :')

huangapple
  • 本文由 发表于 2023年6月12日 21:52:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/76457356.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定