gpt4 book ai didi

javascript - 为什么 readdirSync() 说没有这样的文件或目录?

转载 作者:太空宇宙 更新时间:2023-11-04 03:00:13 24 4
gpt4 key购买 nike

index.js

const fs = require('fs');
const commandFiles = fs.readdirSync('./commands').filter(file => file.endsWith('.js'));

文件系统:

src
-commands
-stuff.js
-config.json
-index.js

错误:

Error: ENOENT: no such file or directory, scandir './commands/'
at Object.readdirSync (fs.js:783:3)

文件夹就在那里,并且里面有文件。我尝试了 './commands/' 、 './commands' 、 'commands' ,似乎没有任何效果。

最佳答案

尝试使用path模块处理路径,如下

const path = require('path');

const dirPath = path.resolve(__dirname, './commands');

然后将dirPath传递给readdirSync函数。path 是一个内部的node.js模块,所以你不需要安装任何东西

关于javascript - 为什么 readdirSync() 说没有这样的文件或目录?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59256354/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com