最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Nodejs path.resolve is not defined - Stack Overflow

programmeradmin3浏览0评论
// 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"));

i am using nodejs v10.8.0 and the directory stucture is

 - project/
 -     debug/
 -         codenotworking.js
 -     public/
 -        js/
 -            file2.js
 -        file.html

whenever i run this code i get the following error

TypeError: path.resolve is not a function at fs.readdir (C:\backup\project\debug\codenotworking.js:17:24) at FSReqWrap.onplete (fs.js:139:20)

what am i doing wrong here ?

// 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"));

i am using nodejs v10.8.0 and the directory stucture is

 - project/
 -     debug/
 -         codenotworking.js
 -     public/
 -        js/
 -            file2.js
 -        file.html

whenever i run this code i get the following error

TypeError: path.resolve is not a function at fs.readdir (C:\backup\project\debug\codenotworking.js:17:24) at FSReqWrap.onplete (fs.js:139:20)

what am i doing wrong here ?

Share Improve this question asked May 23, 2020 at 19:01 KarambitKarambit 3564 silver badges13 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 15

You're shadowing your path import by specifing the path parameter in collectFileNamesRecursively. Change the parameter name to something else.

Apart from that using recursion with callbacks this way won't work - I would remend using async/await. Something like:

const path = require('path');
const fs = require('fs');

async function collectFileNamesRecursively(currBasePath, foundFileNames) {
    const dirContents = await fs.promises.readdir(currBasePath);

    for (const file of dirContents) {
        const currFilePath = path.resolve(currBasePath, file);
        const stat = await fs.promises.stat(currFilePath);
        if (stat.isDirectory()) {
            await collectFileNamesRecursively(currFilePath, foundFileNames);
        } else {
            foundFileNames.push(file);
        }
    }

}
发布评论

评论列表(0)

  1. 暂无评论