首页 > 解决方案 > nodejs fs 路径和 readdirSync

问题描述

我尝试在我的应用程序中使用函数 readdirsync。我想阅读我的应用程序根目录顶部的一个特殊文件夹。

development
--allure 
----allure-result
--myapproot
----myapp.js

我现在想读取文件夹 allure-result 为此我首先使用: watch path = "../allure/allure-results/"

var watchpath = path.join(__alluredir , 'allure-results/');
var dirFiles1 = fs.readdirSync(watchpath);
console.log("Test");
console.log(dirFiles1.length);

然后我尝试了 watch path = "/users/myname/development/allure/allure-results/"

var watchpath = path.join(__watchdir , 'allure-results/');
var dirFiles1 = fs.readdirSync(watchpath);
console.log("Test");
console.log(dirFiles1.length);

该应用程序不会回答这两条 console.log 行。只是沉默。console.log 中没有错误。看起来 readdirSync 不能正常工作。我也试过

var dirFiles1 = await fs.readdirSync(watchpath);

文件夹里面有很多文件。也许这就是问题所在?希望任何人都可以在这里提供帮助,为什么该功能没有结果。

标签: javascriptnode.jsfs

解决方案


这是我的文件夹结构..

项目名称

|-- app.js

|-- 包.json

|-- 实用工具

     |`-- test
          |-- 1.js
          |-- 2.js
          |-- 3.js
   `

|-- 包.json

|-- 节点模块

当我想列出 utils/test/* 下的文件时

const fs=require('fs');
console.log(fs.readdirSync('./utils/test').length) >>> 3

让我知道,如果这个不适合你!


推荐阅读