🎃

[自分メモ用]nodejsで大量のテキストを読み込む

2022/11/04に公開

参考:stackoverflow

streamだけでやれば、行の途中でchunkが生じる。

解決策:readlinecreateInterfaceを導入。

import { createReadStream } from 'fs';
import * as readlinePromises from 'node:readline/promises';

async function readFileByLine(fileName){
  const rs = createReadStream(fileName, {
    encoding: 'utf-8',
  });

  const rl = readlinePromises.createInterface({
    input: rs,
    crlfDelay: Infinity,
  });

  for await (const line of rl) {
    // do something
  }
}

Discussion