On this page

    🌐 Reading files with Node.js

    在 Node.js 中读取文件最简单的方法是使用 fs.readFile() 方法,传入文件路径、编码以及一个回调函数,该函数将在读取文件数据(以及错误)时被调用:

    🌐 The simplest way to read a file in Node.js is to use the fs.readFile() method, passing it the file path, encoding and a callback function that will be called with the file data (and the error):

    const fs = require('node:fs');
    
    fs.readFile('/Users/joe/test.txt', 'utf8', (err, data) => {
      if (err) {
        console.error(err);
        return;
      }
      console.log(data);
    });

    或者,你可以使用同步版本 fs.readFileSync()

    🌐 Alternatively, you can use the synchronous version fs.readFileSync():

    const fs = require('node:fs');
    
    try {
      const data = fs.readFileSync('/Users/joe/test.txt', 'utf8');
      console.log(data);
    } catch (err) {
      console.error(err);
    }

    你也可以使用 fs/promises 模块提供的基于 Promise 的 fsPromises.readFile() 方法:

    🌐 You can also use the promise-based fsPromises.readFile() method offered by the fs/promises module:

    const fs = require('node:fs/promises');
    
    async function example() {
      try {
        const data = await fs.readFile('/Users/joe/test.txt', { encoding: 'utf8' });
        console.log(data);
      } catch (err) {
        console.error(err);
      }
    }
    example();

    fs.readFile()fs.readFileSync()fsPromises.readFile() 这三者都会在返回数据之前将文件的完整内容读入内存。

    🌐 All three of fs.readFile(), fs.readFileSync() and fsPromises.readFile() read the full content of the file in memory before returning the data.

    这意味着大文件将对内存消耗和程序执行速度产生重大影响。

    🌐 This means that big files are going to have a major impact on your memory consumption and speed of execution of the program.

    在这种情况下,更好的选择是使用流读取文件内容。

    🌐 In this case, a better option is to read the file content using streams.

    import fs from 'fs';
    import { pipeline } from 'node:stream/promises';
    import path from 'path';
    
    const fileUrl = 'https://www.gutenberg.org/files/2701/2701-0.txt';
    const outputFilePath = path.join(process.cwd(), 'moby.md');
    
    async function downloadFile(url, outputPath) {
      const response = await fetch(url);
    
      if (!response.ok || !response.body) {
        // consuming the response body is mandatory: https://undici.nodejs.org/#/?id=garbage-collection
        await response.body?.cancel();
        throw new Error(`Failed to fetch ${url}. Status: ${response.status}`);
      }
    
      const fileStream = fs.createWriteStream(outputPath);
      console.log(`Downloading file from ${url} to ${outputPath}`);
    
      await pipeline(response.body, fileStream);
      console.log('File downloaded successfully');
    }
    
    async function readFile(filePath) {
      const readStream = fs.createReadStream(filePath, { encoding: 'utf8' });
    
      try {
        for await (const chunk of readStream) {
          console.log('--- File chunk start ---');
          console.log(chunk);
          console.log('--- File chunk end ---');
        }
        console.log('Finished reading the file.');
      } catch (error) {
        console.error(`Error reading file: ${error.message}`);
      }
    }
    
    try {
      await downloadFile(fileUrl, outputFilePath);
      await readFile(outputFilePath);
    } catch (error) {
      console.error(`Error: ${error.message}`);
    }