TS1232

error TS1232: An import declaration can only be used at the top level of a namespace or module.

Broken Code ❌

1
2
3
4
5
export function dumpPayload() {
const payload = {};
import {writeFileSync} from 'fs';
writeFileSync(`dump-${Date.now()}.json`, JSON.stringify(payload));
}

Fixed Code ✔️

1
2
3
4
5
6
import {writeFileSync} from 'fs';

export function dumpPayload() {
const payload = {};
writeFileSync(`dump-${Date.now()}.json`, JSON.stringify(payload));
}