简体   繁体   English

fs.readdir 没有读取作为参数传递的路径

[英]fs.readdir Not Reading The Path Passed As The Argument

I'm trying to read all files in a directory using fs.我正在尝试使用 fs 读取目录中的所有文件。 When i call the fs.readdir function I get the error当我调用 fs.readdir function 时出现错误

ENOENT: no such file or directory, scandir 'C:\dev\main\file:\C:\dev\main\public' ENOENT:没有这样的文件或目录,scandir 'C:\dev\main\file:\C:\dev\main\public'

When I console log the value I pass to the function it prints当我控制台记录我传递给 function 的值时,它会打印

file:///C:/dev/main/public文件:///C:/dev/main/public

This is the code.这是代码。

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

export let readDir = './';
let files = [];

if (readDir !== './') {
    console.log(url.pathToFileURL(readDir).href);
    fs.readdir(url.pathToFileURL(readDir).href, (err, dir) => {
        if (err){
            throw err;

        }
    })
}

Why is the readdir function not reading from the directory I passed to it?为什么 readdir function 没有从我传递给它的目录中读取?

readdir expects data in form of URL instance or a path as string . readdir需要URL实例形式的数据或string形式的路径。

You pass a string here, so you're defining a path for readdir .你在这里传递一个string ,所以你定义了readdir的路径。 Because your string is not actually a path, you should either因为您的string实际上不是路径,所以您应该

  • use readDir directly (as it seems to be a path based on your code)直接使用readDir (因为它似乎是基于您的代码的路径)
  • or use a new url.URL(url.pathToFileURL(readDir))或使用new url.URL(url.pathToFileURL(readDir))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM