Nodejs FS模块不返回此类文件或目录错误 [英] Nodejs FS module returning no such file or dir error
问题描述
代码:
fs.readdir('./commands/', (err, files) => {
// Do something... });
Code:
fs.readdir('./commands/', (err, files) => {
// Do something... });
错误:
ENOENT: no such file or directory, scandir './commands/'
Error:
ENOENT: no such file or directory, scandir './commands/'
文件夹./commands/
确实存在.该文件是src/index.js
,并且正在尝试读取src/commands/
的目录.它不是fs.readdir('/commands'
,因为那将指向我PC的根目录(Ubuntu 18.04 LTS,Node版本v8.10.0).如果需要其他信息,请询问,我会提供.
The folder ./commands/
does exist. This file is src/index.js
and is trying to read the directory of src/commands/
. It wouldn't be fs.readdir('/commands'
because that would be referring to the root directory of my PC (Ubuntu 18.04 LTS, Node version v8.10.0). If any futher information is required, ask and I will provide.
谢谢大家.
推荐答案
尝试使用__dirname:
try with __dirname:
fs.readdir(__dirname +'/commands/', (err, files) => {
// Do something...
})
这篇关于Nodejs FS模块不返回此类文件或目录错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!