TS1431

error TS1431: ‘for await’ loops are only allowed at the top level of a file when that file is a module, but this file has no imports or exports. Consider adding an empty ‘export {}’ to make this file a module.

Broken Code ❌

1
2
3
4
5
6
7
8
9
10
const inputStream = fs.createReadStream('file.csv');

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

for await (const line of rl) {
console.log(line);
}

Fixed Code ✔️

Just add the imports for the fs module and readline module in order to turn your code into a module itself:

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

const inputStream = fs.createReadStream('file.csv');

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

for await (const line of rl) {
console.log(line);
}