You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Node.js中使用fs.readFile读取文件出现ENOENT错误求助

问题解决:读取HTML文件生成对应TXT文件时的ENOENT错误

错误原因分析

你遇到的ENOENT错误是脚本找不到指定路径的文件,核心原因通常有两个:

  • src/html目录下确实不存在a.html/b.html,或文件名拼写、大小写不匹配(Windows系统虽不区分大小写,但代码中最好与实际文件名完全一致)
  • 脚本运行时的相对路径解析错误:你代码中用的html/a.html是相对于运行node命令时的工作目录,而非generate.js所在的src目录。比如如果在projectRepo目录下执行node src/generate.js,脚本会去projectRepo/html/a.html找文件,而实际文件在projectRepo/src/html/a.html,自然找不到。

解决方案

第一步:确认文件存在

先检查src/html目录下是否真的存在目标HTML文件,核对文件名的拼写、大小写是否完全一致。

第二步:修复路径并实现完整功能

使用__dirname(当前脚本所在目录的绝对路径)拼接文件路径,确保无论在哪里运行脚本,路径都能正确解析。以下是完整实现代码:

基础版(仅处理一级目录)

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

// 拼接html目录和text目录的绝对路径
const htmlDir = path.join(__dirname, "html");
const textDir = path.join(__dirname, "text");

// 先确保text目录存在,不存在则创建
if (!fs.existsSync(textDir)) {
  fs.mkdirSync(textDir, { recursive: true });
}

// 读取html目录下的所有文件
fs.readdir(htmlDir, { withFileTypes: true }, (err, entries) => {
  if (err) throw err;

  entries.forEach(entry => {
    if (entry.isFile() && path.extname(entry.name) === ".html") {
      const htmlFilePath = path.join(htmlDir, entry.name);
      // 读取HTML文件内容
      fs.readFile(htmlFilePath, "utf8", (err, data) => {
        if (err) throw err;

        // 生成对应TXT文件的路径
        const txtFileName = path.basename(entry.name, ".html") + ".txt";
        const txtFilePath = path.join(textDir, txtFileName);

        // 写入TXT文件
        fs.writeFile(txtFilePath, data, "utf8", (err) => {
          if (err) throw err;
          console.log(`已生成文件:${txtFilePath}`);
        });
      });
    }
  });
});

递归版(支持多级子目录)

如果html目录下有子文件夹(如html/sub/c.html),需要生成对应结构的text/sub/c.txt,可使用递归遍历版本:

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

const htmlRootDir = path.join(__dirname, "html");
const textRootDir = path.join(__dirname, "text");

// 递归处理目录
function processDir(currentHtmlDir, currentTextDir) {
  // 创建对应text子目录
  if (!fs.existsSync(currentTextDir)) {
    fs.mkdirSync(currentTextDir, { recursive: true });
  }

  fs.readdir(currentHtmlDir, { withFileTypes: true }, (err, entries) => {
    if (err) throw err;

    entries.forEach(entry => {
      const htmlPath = path.join(currentHtmlDir, entry.name);
      const textPath = path.join(currentTextDir, entry.name);

      if (entry.isDirectory()) {
        // 递归处理子目录
        processDir(htmlPath, textPath);
      } else if (entry.isFile() && path.extname(entry.name) === ".html") {
        // 处理HTML文件,生成对应TXT
        const txtFilePath = path.join(currentTextDir, path.basename(entry.name, ".html") + ".txt");
        fs.readFile(htmlPath, "utf8", (err, data) => {
          if (err) throw err;
          fs.writeFile(txtFilePath, data, "utf8", (err) => {
            if (err) throw err;
            console.log(`已生成文件:${txtFilePath}`);
          });
        });
      }
    });
  });
}

// 启动处理
processDir(htmlRootDir, textRootDir);

运行注意事项

无论使用哪个版本,你可以:

  • 进入src目录执行node generate.js
  • 或在projectRepo目录执行node src/generate.js
    因为用了__dirname,路径都会自动正确解析。

内容的提问来源于stack exchange,提问作者user2613946

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.26 16:57:40