Fixing TS1192 “Module has no default export” in TypeScript
error TS1192: Module '"…"' has no default export. appears when you use a default import, such as import calc from "./math", on a file or package that has no default export. The error goes away if you import by the names the module actually exports, such as import { add } from "./math". If you wrote the module yourself, adding export default to it also makes the error go away.
This article is about the case where tsc's type check, or the Problems panel (PROBLEMS) in VS Code, shows this error on a line that imports a file of functions or an npm library. I reproduced the error in my test environment and included the exact messages as they appeared. I also confirmed that the fix removes the error, and what happens when you run the output with the error still present in Node.js.
The test environment was Ubuntu 24.04, Node.js 22.23.4, and npm 10.9.9. I reproduced the error with TypeScript 5.9.3 and confirmed that TypeScript 6.0.3 and 7.0.2 print the same message. For the package example I used nanoid 5.1.16. I did not open the VS Code UI itself. Instead, I queried tsserver, the server VS Code uses internally, and confirmed that it returns the same diagnostics. I did not test importing a CommonJS package that uses export =.
- 1. The message when default-importing a file you wrote
- 2. Fix 1: Use a named import
- 3. Fix 2: Add export default to the module being imported
- 4. When default-importing an npm package
- 5. allowSyntheticDefaultImports and esModuleInterop do not remove the error
- 6. The same error is returned by tsserver, which VS Code uses
The message when default-importing a file you wrote
I installed TypeScript 5.9.3 in a project created with npm init -y and added "type": "module" to package.json. The tsconfig.json is shown below. The bundler setting for moduleResolution is meant for use with a bundler.
{
"compilerOptions": {
"module": "esnext",
"moduleResolution": "bundler",
"target": "es2022",
"strict": true,
"outDir": "dist",
"rootDir": "src"
},
"include": ["src"]
}
src/math.ts only exports the addition function add as a named export. It has no export default.
// src/math.ts
export function add(a: number, b: number): number {
return a + b;
}
In src/index.ts, I default-imported this under the name calc.
// src/index.ts
import calc from "./math";
console.log(calc(1, 2));
Running npx tsc --noEmit printed the single line below and exited with code 2. /path/to/app is the project location. I replaced the path from my test environment with this placeholder in the actual output (the same applies below).
$ npx tsc --noEmit
src/index.ts(1,8): error TS1192: Module '"/path/to/app/src/math"' has no default export.
With --pretty, the output looks like this. The squiggly line appeared under calc (colors omitted).
src/index.ts:1:8 - error TS1192: Module '"/path/to/app/src/math"' has no default export.
1 import calc from "./math";
~~~~
Found 1 error in src/index.ts:1
The message means that the module …/src/math has no default export. The import calc from form asks for the module's default export. The MDN explanation of import also says that named imports and default imports are different syntax. Since math.ts has no default export to import, TypeScript stops here.
Importing with the same name as the export gives TS2613
When I used import add from "./math", which has the same name as the module's named export, the error code changed to TS2613.
$ npx tsc --noEmit
src/index.ts(1,8): error TS2613: Module '"/path/to/app/src/math"' has no default export. Did you mean to use 'import { add } from "/path/to/app/src/math"' instead?
The second half is a hint: "Did you mean to use import { add } from …?" I looked at reportNonDefaultExport in checker.ts in the TypeScript 5.9.3 source. It emits TS2613 when the module has a named export with the imported name, and TS1192 without the hint when it does not. The cause and the fix are the same in both cases.
Fix 1: Use a named import
Change the import to match the name the module exports.
import { add } from "./math";
console.log(add(1, 2));
npx tsc --noEmit printed nothing and exited with code 0. If you want to keep the name calc, rename it with as.
import { add as calc } from "./math";
console.log(calc(1, 2));
This also exited with code 0. If you want to load all the exported functions together, import * as math from "./math" and calling math.add(1, 2) also exited with code 0.
As the MDN explanation of export describes, named exports are imported by the same name (you can rename them with as), while a default export can be imported under any name. That is why the error does not go away no matter what name you give the X in import X from.
Fix 2: Add export default to the module being imported
If you wrote math.ts yourself, you can also add a default export to it.
// src/math.ts
export function add(a: number, b: number): number {
return a + b;
}
export default add;
With this change, src/index.ts can keep the original import calc from "./math";, and npx tsc --noEmit exited with code 0. However, you should not edit the contents of an npm package yourself, so for packages use Fix 1.
When default-importing an npm package
I tested this with nanoid 5.1.16, a library that generates IDs. Its type definitions (index.d.ts) export functions such as nanoid and customAlphabet as named exports and have no export default.
import makeId from "nanoid";
console.log(makeId().length);
$ npx tsc --noEmit
src/index.ts(1,8): error TS1192: Module '"/path/to/app/node_modules/nanoid/index"' has no default export.
For a package, the message shows the location of the type definition in node_modules instead of a file path. With import nanoid from "nanoid", the error code was TS2613, and the hint suggested using import { nanoid } from ….
Running the output with the error still present stops in Node.js
tsc emits JavaScript even when there are type errors. This is because the default for noEmitOnError, which stops output when there are errors, is false, as described in the TSConfig reference for noEmitOnError. When I ran npx tsc, it printed TS1192 and still created dist/index.js. The import line was left as it was.
import makeId from "nanoid";
console.log(makeId().length);
Running this with node dist/index.js stopped with the error below and exited with code 1. I omitted the file-location line at the top and the rest of the stack trace.
import makeId from "nanoid";
^^^^^^
SyntaxError: The requested module 'nanoid' does not provide an export named 'default'
This is the Node.js error for a default export that cannot be found. TS1192 reports this failure during the type check.
After changing the line to import { nanoid } from "nanoid", npx tsc --noEmit exited with code 0. Running the generated JavaScript with node dist/index.js printed the length of the generated ID, 21, and exited with code 0. Renaming it to import { nanoid as makeId } from "nanoid" also printed 21.
allowSyntheticDefaultImports and esModuleInterop do not remove the error
When I searched for the error "cannot import with import X from," the results mentioned allowSyntheticDefaultImports and esModuleInterop in tsconfig.json. For importing math.ts, enabling either option did not remove TS1192. Importing nanoid with import makeId from "nanoid" behaved the same way.
$ npx tsc --noEmit --allowSyntheticDefaultImports true
src/index.ts(1,8): error TS1192: Module '"/path/to/app/src/math"' has no default export.
$ npx tsc --noEmit --esModuleInterop true
src/index.ts(1,8): error TS1192: Module '"/path/to/app/src/math"' has no default export.
In this tsconfig.json, allowSyntheticDefaultImports was already true from the start, as npx tsc --showConfig shows, and the error still appeared. The TSConfig reference for allowSyntheticDefaultImports gives as its example importing a CommonJS file that writes its output with module.exports. For ES Modules files or packages written with export that have no default export, you need to fix the import syntax.
The same error is returned by tsserver, which VS Code uses
According to the VS Code TypeScript page, VS Code analyzes code with the TypeScript language service and shows errors in the editor and in the Problems panel. The page on using the workspace version of TypeScript explains that you specify the TypeScript used by VS Code by pointing a setting to the folder that contains tsserver.js. tsserver is the server that provides the TypeScript compiler and language service to editors.
Because I could not open the VS Code UI in my test environment, I started the tsserver included with TypeScript 5.9.3 instead. I wrote a small script that sends diagnostic requests in JSON, and I used it to check a file with the three imports from above. The script prints the diagnostics it gets back.
import calc from "./math";
import add from "./math";
import makeId from "nanoid";
console.log(calc(1, 2), add(1, 2), makeId());
The diagnostics returned had the same codes and messages as tsc (the script prints line:column, code, and message in that order).
diagnostic: 1:8 TS1192 Module '"/path/to/app/src/math"' has no default export.
diagnostic: 2:8 TS2613 Module '"/path/to/app/src/math"' has no default export. Did you mean to use 'import { add } from "/path/to/app/src/math"' instead?
diagnostic: 3:8 TS1192 Module '"/path/to/app/node_modules/nanoid/index"' has no default export.
When I changed the three imports to import { add as calc } from "./math", import { add } from "./math", and import { nanoid as makeId } from "nanoid" and queried again, no diagnostics were returned.
VS Code normally uses the TypeScript bundled with it. As the VS Code FAQ explains, its version can differ from the TypeScript installed in your project. So I also checked the same file with tsc from TypeScript 6.0.3 and 7.0.2. Both printed the same three errors with the same messages as above. The exit codes were 2 for 6.0.3 and 1 for 7.0.2. The error did not disappear in any version I tested, so whether you use VS Code or tsc, the fix is to change the import syntax.





