TS1432

error TS1432: Top-level ‘for await’ loops are only allowed when the ‘module’ option is set to ‘es2022’, ‘esnext’, ‘system’, ‘node16’, or ‘nodenext’, and the ‘target’ option is set to ‘es2017’ or higher.

Broken Code ❌

start.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
import fs from 'node:fs';
import readline from 'node:readline';

const file = 'abc.csv';

const fileStream = fs.createReadStream(file);

const rl = readline.createInterface({
input: fileStream,
crlfDelay: Infinity
});

for await (const line of rl) {
console.log(`Line from file: ${line}`);
}
tsconfig.json
1
2
3
4
5
6
7
{
"compilerOptions": {
"lib": ["es2017"],
"module": "commonjs",
"target": "es6"
}
}

Fixed Code ✔️

The error is similar to TS1378 and needs an adjustment in the tsconfig.json file:

tsconfig.json
1
2
3
4
5
6
7
{
"compilerOptions": {
"lib": ["es2017"],
"module": "esnext",
"target": "es2017"
}
}