Nodejs FS模块不返回此类文件或目录错误 [英] Nodejs FS module returning no such file or dir error

查看:159
本文介绍了Nodejs FS模块不返回此类文件或目录错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

代码: 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆