Fixing TS2306 “File is not a module” in TypeScript Imports
error TS2306: File '…/utils.ts' is not a module. appears when the file you import from has neither import nor export. TypeScript treats that file as a script, not a module, so nothing can be imported from it. Adding export to the declaration of the function or type you want to use makes the error go away.
If you split functions or types into a separate file, load them with import { add } from "./utils";, and tsc or an editor such as VS Code reports "is not a module", you can fix it with the steps in this article. I reproduced the error in my test environment and quoted the message as is. I confirmed that the error disappears after the fix, both with tsc and with the language server used by editors.
The test environment is Ubuntu 24.04, Node.js 22.23.4, npm 10.9.9, and TypeScript 7.0.2. For the editor results, I did not use an editor such as VS Code itself. Instead, I ran the TypeScript 7 language server directly (tsc --lsp --stdio). I did not test how this looks in VS Code, versions of TypeScript before 7, or loading .js files with allowJs.
- 1. Importing a file without export causes an error
- 2. A file without import or export is not a module
- 3. Fix: add export to the function you want to use
- 4. Adding export {} alone changes it to a different error
- 5. In a project created by tsc –init, the same code gives TS2459
- 6. Importing a type written in a .d.ts file also triggers the error
Importing a file without export causes an error
I ran npm init -y in an empty folder and installed TypeScript 7.0.2. The tsconfig.json has the following content.
{
"compilerOptions": {
"module": "esnext",
"moduleResolution": "bundler",
"target": "es2022",
"strict": true,
"noEmit": true
},
"include": ["src"]
}
I wrote an addition function in src/utils.ts and left off export.
// src/utils.ts
function add(a: number, b: number): number {
return a + b;
}
I import this function from src/main.ts.
// src/main.ts
import { add } from "./utils";
console.log(add(1, 2));
When I ran npx tsc, the following error appeared and the exit code was 1. In the message, I replaced the work folder path in my test environment with /path/to/app.
$ npx tsc
src/main.ts(1,21): error TS2306: File '/path/to/app/src/utils.ts' is not a module.
With --pretty, you can see that the error location is the "./utils" part (colors omitted).
src/main.ts:1:21 - error TS2306: File '/path/to/app/src/utils.ts' is not a module.
1 import { add } from "./utils";
~~~~~~~~~
Found 1 error in src/main.ts:1
The error is reported in src/main.ts, where the import is written. The message shows the path of src/utils.ts, the file being loaded. The file to fix is the one at this path.
When I had the language server open src/main.ts, it returned one diagnostic with the same content. The output below comes from a small script I wrote. It formats the language server's response into one line per diagnostic and counts lines and columns from 1. The path is replaced in the same way. 1:21-1:30 is the range of "./utils" on line 1.
diagnostics (1):
1:21-1:30 severity=1 source=ts code=2306 File '/path/to/app/src/utils.ts' is not a module.
TS2306 was also reported with the same message when I changed the import style, for example to import utils from "./utils"; or import * as utils from "./utils";. On the other hand, a side-effect-only import such as import "./utils";, which loads the file without taking anything out of it, produced no error.
A file without import or export is not a module
The modules chapter of the TypeScript Handbook says that a file with a top-level import or export is treated as a module, and a file with neither is treated as a script, whose contents are visible in the global scope.
src/utils.ts has neither import nor export, so it is a script. A script exports nothing, so there is nothing to take out with import { add } from "./utils". TS2306 tells you that you are trying to import from a file that is not a module.
Fix: add export to the function you want to use
Add export in front of the function in src/utils.ts.
// src/utils.ts
export function add(a: number, b: number): number {
return a + b;
}
npx tsc printed nothing and exited with code 0. The language server also returned zero diagnostics. Because of export, src/utils.ts became a module, and add can now be imported.
Adding export {} alone changes it to a different error
The handbook suggests adding one line, export {};, when you want to treat a file with neither import nor export as a module. However, that is a way to make a module that exports nothing.
I added only export {}; to the end of src/utils.ts and tried it without export on add.
// src/utils.ts
function add(a: number, b: number): number {
return a + b;
}
export {};
TS2306 disappeared, but TS2459 appeared instead. It means that ./utils declares add locally but does not export it.
$ npx tsc
src/main.ts(1,10): error TS2459: Module '"./utils"' declares 'add' locally, but it is not exported.
If another file uses the function, you still need to add export to add itself.
In a project created by tsc –init, the same code gives TS2459
In a tsconfig.json created by npx tsc --init, the same code gave TS2459 instead of TS2306. The tsconfig.json generated by tsc --init in TypeScript 7.0.2 contained the following two lines.
$ npx tsc --init > /dev/null && grep -n -E "\"module\"|moduleDetection" tsconfig.json
10: "module": "nodenext",
41: "moduleDetection": "force",
I changed src/main.ts to include the extension: import { add } from "./utils.js";. src/utils.ts is the first version, without export.
$ npx tsc --noEmit
src/main.ts(1,10): error TS2459: Module '"./utils.js"' declares 'add' locally, but it is not exported.
According to the description of moduleDetection, "force" treats every file except declaration files (.d.ts) as a module. src/utils.ts becomes a module that exports nothing, so the message changes to TS2459. When I ran the same project with --moduleDetection auto, the message went back to TS2306. package.json has no "type" field in this project.
$ npx tsc --noEmit --moduleDetection auto
src/main.ts(1,21): error TS2306: File '/path/to/app-node/src/utils.ts' is not a module.
In the project with module set to esnext that I created at the beginning, adding --moduleDetection force also gave TS2459. In both cases, the fix is to add export to add. However, in the project created by tsc --init, adding export alone was not enough. When I added export to add without a "type" field in package.json, the following two errors appeared.
$ npx tsc --noEmit
src/main.ts(1,10): error TS1295: ECMAScript imports and exports cannot be written in a CommonJS file under 'verbatimModuleSyntax'. Adjust the 'type' field in the nearest 'package.json' to make this file an ECMAScript module, or adjust your 'verbatimModuleSyntax', 'module', and 'moduleResolution' settings in TypeScript.
src/utils.ts(1,1): error TS1287: A top-level 'export' modifier cannot be used on value declarations in a CommonJS module when 'verbatimModuleSyntax' is enabled.
This tsconfig.json also had "verbatimModuleSyntax": true. When package.json has no "type" field, files are treated as CommonJS. Because verbatimModuleSyntax is enabled, import and export cannot be written in those files as they are. Following the advice in the message, I added "type": "module" to package.json with npm pkg set type=module. After that, npx tsc --noEmit exited with code 0.
Importing a type written in a .d.ts file also triggers the error
The same thing happens with a declaration file that contains only types. I wrote a type in src/types.d.ts without export.
// src/types.d.ts
interface User {
id: number;
name: string;
}
I import it from src/profile.ts.
// src/profile.ts
import { User } from "./types";
const user: User = { id: 1, name: "neko" };
console.log(user);
$ npx tsc
src/profile.ts(1,22): error TS2306: File '/path/to/app/src/types.d.ts' is not a module.
Declaration files are excluded from "force" in moduleDetection, so TS2306 appeared even with --moduleDetection force. There are two fixes, and each made npx tsc exit with code 0.
The first is to add export to the interface in src/types.d.ts and import it.
// src/types.d.ts
export interface User {
id: number;
name: string;
}
The second is to leave src/types.d.ts without export and remove the import line from src/profile.ts. A type in a declaration file without export is in the global scope, so you can use User without importing it.
// src/profile.ts
const user: User = { id: 1, name: "neko" };
console.log(user);
Do not mix the two fixes. When you add export to src/types.d.ts, User is no longer a global type. In my test, a file that used User without an import got error TS2304: Cannot find name 'User'. When you add export to a type that some file uses without importing it, add an import to that file as well.





