TS2306

error TS2306: File ‘add.ts‘ is not a module.

Broken Code ❌

add.ts
1
2
3
function add(a: number, b: number): number {
return a + b;
}
main.ts
1
2
3
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:

add.ts
1
2
3
export function add(a: number, b: number): number {
return a + b;
}

Alternatively we can use a default export:

add.ts
1
2
3
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):

main.ts
1
2
3
import add from './add';

console.log(add(1000, 337));