TS2306
File 'add.ts' is not a module.
Broken Code ❌
function add(a: number, b: number): number {
return a + b;
}import { add } from './add';
console.log(add(1000, 337));Fixed Code ✔️
The error TS2306 signals that the file (add.ts) can be found (otherwise it would throw error TS2307) but does not provide the necessary exports. We can solve this with a named export:
export function add(a: number, b: number): number {
return a + b;
}Alternatively, we can use a default export:
export default function add(a: number, b: number): number {
return a + b;
}Using a default export requires that we also adjust our import statement in main.ts (otherwise we would end up with error TS2614):
import add from './add';
console.log(add(1000, 337));