日本語

How to Fix TS1208 in TypeScript (isolatedModules Error)

​​TypeScript​Troubleshooting

error TS1208: ... cannot be compiled under '--isolatedModules' because it is considered a global script file. appears when a project has isolatedModules enabled in tsconfig.json and a .ts file has no import or export. Making the file a module clears it. There are two ways: add export {}; to the file, or set moduleDetection to "force" in tsconfig.json.

Vite recommends setting isolatedModules to true, because it transpiles one file at a time without type information. Below is how I fixed this error when running a type check with tsc in such a project.

Note that this error did not appear for the same files in TypeScript 5.0.2, 5.9.3, and 7.0.2.

Note

My test environment was Ubuntu 24.04, Node.js 22.23.4, and npm 10.9.9. I reproduced TS1208 with TypeScript 4.9.5. I only checked the output of tsc. I did not test the display in editors such as VS Code, versions earlier than 4.9.5, or builds with build tools such as Vite.

Reproducing the error with a file that has no import or export

I ran npm init -y in an empty folder and installed TypeScript 4.9.5. The tsconfig.json was as follows. I set isolatedModules to true, and I also added noEmit so that only the type check runs.

json
{
  "compilerOptions": {
    "target": "es2020",
    "module": "esnext",
    "moduleResolution": "node",
    "strict": true,
    "isolatedModules": true,
    "noEmit": true
  },
  "include": ["src"]
}

src/main.ts had only two lines, with no import or export.

ts
const message: string = "hello";
console.log(message);

When I ran npx tsc, the following error appeared, and the exit code was 2.

console
$ npx tsc
src/main.ts(1,1): error TS1208: 'main.ts' cannot be compiled under '--isolatedModules' because it is considered a global script file. Add an import, export, or an empty 'export {}' statement to make it a module.

The message means: "'main.ts' cannot be compiled under --isolatedModules because it is considered a global script file. Add an import or export, or an empty export {} statement, to make it a module." The output with --pretty is shown below (colors removed).

text
src/main.ts:1:1 - error TS1208: 'main.ts' cannot be compiled under '--isolatedModules' because it is considered a global script file. Add an import, export, or an empty 'export {}' statement to make it a module.

1 const message: string = "hello";
  ~~~~~


Found 1 error in src/main.ts:1

The error is at line 1, column 1. With --pretty, only a ~ underline appears under the first word. It is not tied to a particular line of code. It is reported because the file has no import or export at all.

It also appears for empty files and type-only files

I added the following three .ts files and one .d.ts file to the same project:

  • src/empty.ts: completely empty (0 bytes)
  • src/comment.ts: only one line, // import { format } from "./format"; (an import that was commented out)
  • src/types.ts: only interface User { name: string; }
  • src/global.d.ts: only declare const APP_VERSION: string;

When I ran npx tsc, every .ts file got TS1208.

console
$ npx tsc
src/comment.ts(1,1): error TS1208: 'comment.ts' cannot be compiled under '--isolatedModules' because it is considered a global script file. Add an import, export, or an empty 'export {}' statement to make it a module.
src/empty.ts(1,1): error TS1208: 'empty.ts' cannot be compiled under '--isolatedModules' because it is considered a global script file. Add an import, export, or an empty 'export {}' statement to make it a module.
src/main.ts(1,1): error TS1208: 'main.ts' cannot be compiled under '--isolatedModules' because it is considered a global script file. Add an import, export, or an empty 'export {}' statement to make it a module.
src/types.ts(1,1): error TS1208: 'types.ts' cannot be compiled under '--isolatedModules' because it is considered a global script file. Add an import, export, or an empty 'export {}' statement to make it a module.

The error also appears for a file you just created and have not written anything in yet, or for a file where you commented out an import while testing. global.d.ts did not get the error.

TypeScript treats a file with at least one import or export statement as a module. A file with neither is treated as a script that runs in the global scope (TypeScript 4.7 release notes). According to the isolatedModules description, this option makes TypeScript warn about code that tools which transpile one file at a time cannot handle correctly. It does not change how the code runs, nor how TypeScript type-checks or emits.

Making the file a module by adding export {} at the end

As the error message suggests, I added export {}; to the end of the file.

ts
const message: string = "hello";
console.log(message);

export {};

npx tsc printed nothing and exited with code 0. export {}; exports nothing, but because it is an export statement, TypeScript treats the file as a module.

This is a quick fix when only a few files have the error, or when you have an empty file you are just holding onto temporarily. However, you have to write it every time you create a file.

Treating all files as modules in tsconfig.json

Without touching the files, I added "moduleDetection": "force" to compilerOptions in tsconfig.json.

json
{
  "compilerOptions": {
    "target": "es2020",
    "module": "esnext",
    "moduleResolution": "node",
    "strict": true,
    "isolatedModules": true,
    "moduleDetection": "force",
    "noEmit": true
  },
  "include": ["src"]
}

With the original src/main.ts, which has no export {};, npx tsc exited with code 0.

moduleDetection was added in TypeScript 4.7. According to the moduleDetection description, "force" treats every file except declaration files (.d.ts) as a module. If you write all your project files as modules, this option saves you from handling each file individually. TypeScript versions before 4.7 do not support it, so in that case add export {}; to the files instead.

Warning

Setting isolatedModules to false also clears TS1208 (npx tsc --isolatedModules false exited with code 0). However, the warnings about code that one-file-at-a-time tools cannot handle correctly also stop appearing. In projects that use Vite or similar tools, keep isolatedModules set to true and fix the error with one of the two methods above.

TS1208 did not appear in TypeScript 5.0 and later

I tested the same src/main.ts (without export {};) with TypeScript 5.0.2, 5.9.3, and 7.0.2. With 5.0.2 and 5.9.3, npx tsc exited with code 0 using the same tsconfig.json as 4.9.5, and TS1208 did not appear. With 7.0.2, moduleResolution set to node was not accepted and it stopped with a TS5108 error. So for all three, I changed only moduleResolution to bundler and tested again. All of them exited with code 0.

I also checked inside the installed packages. The lib/tsc.js in 4.9.5 contained the TS1208 message (considered a global script file) in one place, but I could not find it in lib/tsc.js in 5.0.2. In the TypeScript repository, a pull request that added verbatimModuleSyntax was merged into the main branch in January 2023. Within it is a commit titled "Add tests for loosened isolatedModules script checks", which relates to relaxing the isolatedModules checks for scripts.

If you see TS1208 in your project, run npx tsc -v to check the TypeScript version. Knowing whether it is before 5.0 will help you find the cause. In 4.9.5, which I tested, both export {}; and moduleDetection cleared the error.

Even on 5.0 and later, a script with a namespace gives TS1280

Even on 5.0 and later, a file with no import or export that contains a namespace gives a different error. I set src/util.ts to the following.

ts
namespace Util {
  export const version = 1;
}
console.log(Util.version);

With 5.0.2, 5.9.3, and 7.0.2, all three showed the following error (the exit code was 2 for 5.x and 1 for 7.0.2).

console
$ npx tsc
src/util.ts(1,11): error TS1280: Namespaces are not allowed in global script files when 'isolatedModules' is enabled. If this file is not intended to be a global script, set 'moduleDetection' to 'force' or add an empty 'export {}' statement.

The message means: "Namespaces are not allowed in global script files when isolatedModules is enabled. If this file is not intended to be a global script, set moduleDetection to force or add an empty export {} statement." The isolatedModules description also states that with isolatedModules enabled, namespaces can only be written in modules that have an import or export.

The fix is the same as for TS1208. Adding export {}; to the end of src/util.ts made all three versions exit with code 0. Adding "moduleDetection": "force" to tsconfig.json without writing export {}; also exited with code 0.

​Posted by 86