// codenotworking
const path = require("path");
const fs = require("fs");
log = console.log;
const names = [];
function collectFileNamesRecursively(path) {
fs.readdir(path, (err, files) => {
err ? log(err) : log(files);
// replacing paths
for (const index in files) {
const file = files[index];
files[index] = path.resolve(path, file);
}
for (let file of files) {
fs.stat(file, (err, stat) => {
err ? log(err) : null;
if (stat.isDirectory()) {
collectFileNamesRecursively(file);
}
names.push(file);
});
}
});
}
collectFileNamesRecursively(path.join(__dirname, "../public"));
我正在使用节点js v10.8.0,目录结构为
- project/
- debug/
- codenotworking.js
- public/
- js/
- file2.js
- file.html
每当我运行此代码时,都会出现以下错误
TypeError:path.resolve不是函数 在fs.readdir(C:\ backup \ project \ debug \ codenotworking.js:17:24) 在FSReqWrap.oncomplete(fs.js:139:20)
我在这里做错了什么?
You're shadowing your
path
import by specifing thepath
parameter incollectFileNamesRecursively
. Changing the parameter name to something else should work: