日本語

Fixing TS2305 “has no exported member” in TypeScript Imports

​​TypeScript​ES Modules,Node.js,Troubleshooting

error TS2305: Module '"./math.js"' has no exported member 'subtract'. appears when a named import such as import { subtract } from "./math.js" loads a name that the other file or package does not export. Adding that export to the module being imported, or changing the import name to one the other side does export, makes the error go away.

These are the causes and fixes for this error, which tsc reported during type checking after I split functions into separate files and after I imported functions from a library installed with npm. The errors shown here are ones I actually triggered in my test environment. I also confirmed that the fixes clear the error and checked how Node.js stops when the error is left in place and the code runs anyway.

Note

My test environment was Ubuntu 24.04, Node.js 22.23.5, and npm 10.9.9. I triggered the error with TypeScript 7.0.2. I confirmed that TS2305, TS2724, and TS2459 also produce the same messages with 5.9.3 and 6.0.3. The package examples use nanoid 5.1.16. I only checked the output of tsc and running the code in Node.js. I did not test how editors such as VS Code display these errors.

The message when you import a name that isn't exported

I created a project with npm init -y, installed TypeScript 7.0.2, and added "type": "module" to package.json. To run the emitted JavaScript directly in Node.js, I set module to nodenext in tsconfig.json.

json
{
  "compilerOptions": {
    "module": "nodenext",
    "target": "es2022",
    "strict": true,
    "outDir": "dist",
    "rootDir": "src"
  },
  "include": ["src"]
}

src/math.ts exports only the addition function add.

ts
// src/math.ts
export function add(a: number, b: number): number {
  return a + b;
}

In src/index.ts, I imported add along with subtract, a subtraction function that does not exist in math.ts.

ts
// src/index.ts
import { add, subtract } from "./math.js";

console.log(add(1, 2), subtract(3, 1));

Running npx tsc --noEmit printed one line and exited with code 1.

console
$ npx tsc --noEmit
src/index.ts(1,15): error TS2305: Module '"./math.js"' has no exported member 'subtract'.

With --pretty true, the squiggle appeared under subtract (colors omitted here).

text
src/index.ts:1:15 - error TS2305: Module '"./math.js"' has no exported member 'subtract'.

1 import { add, subtract } from "./math.js";
                ~~~~~~~~


Found 1 error in src/index.ts:1

The message means that module ./math.js has no exported member named subtract. As the MDN explanation of import describes, a named import loads a value that the other module exports under that name. Since math.ts does not export subtract, TypeScript reports it as an error.

If you emit anyway, Node.js stops

tsc emits JavaScript even when there are type errors, because the default for noEmitOnError is false (TSConfig reference for noEmitOnError). When I ran npx tsc, it printed TS2305 and still created dist/index.js and dist/math.js. The import line remained as is in dist/index.js.

js
import { add, subtract } from "./math.js";
console.log(add(1, 2), subtract(3, 1));

Running node dist/index.js stopped with the following error and exited with code 1. In the first line, I replaced the path of my test environment with /path/to/app. I omitted the rest of the stack trace.

text
file:///path/to/app/dist/index.js:1
import { add, subtract } from "./math.js";
              ^^^^^^^^
SyntaxError: The requested module './math.js' does not provide an export named 'subtract'

This is the Node.js error saying the module being loaded does not have a subtract export. TS2305 reports the same failure at type-check time.

Fix 1: Add the export to the module being imported

If you really need subtract and you wrote math.ts yourself, add the function there and export it.

ts
// src/math.ts
export function add(a: number, b: number): number {
  return a + b;
}

export function subtract(a: number, b: number): number {
  return a - b;
}

src/index.ts stayed the same. npx tsc --noEmit printed nothing and exited with code 0. Running the emitted JavaScript with node dist/index.js printed 3 2.

If you forget the export keyword, the error becomes TS2459

I added subtract to math.ts but left off export. The error code changed to TS2459.

ts
// src/math.ts
export function add(a: number, b: number): number {
  return a + b;
}

function subtract(a: number, b: number): number {
  return a - b;
}
console
$ npx tsc --noEmit
src/index.ts(1,15): error TS2459: Module '"./math.js"' declares 'subtract' locally, but it is not exported.

The message means that subtract is declared inside the file but not exported. Adding export before function, or writing export { subtract }; at the end of the file, clears the error. As the MDN explanation of export describes, export { … } exports names that were declared elsewhere. After I added export { subtract };, npx tsc --noEmit exited with code 0, and running the code printed 3 2.

Fix 2: Match the import name to what the other side exports

If you cannot change the module being imported, or if the name was wrong from the start, correct the import name.

A similar name gives TS2724 with a suggestion

When I wrote import { Add } from "./math.js" with the wrong capitalization, the error was TS2724 instead of TS2305, and it suggested the correct name.

console
$ npx tsc --noEmit
src/index.ts(1,10): error TS2724: '"./math.js"' has no exported member named 'Add'. Did you mean 'add'?

The last part, Did you mean 'add'?, asks whether you meant add. Changing the import to add loads an export that math.ts does provide, so the exit code became 0, as in the Fix 1 example. For a name with no similar export, such as subtract here, the result was TS2305 with no suggestion.

If the other side has only a default export, you get TS2614

When math.ts has only a default export, as in export default function add, writing import { add } from "./math.js" gave TS2614.

console
$ npx tsc --noEmit
src/index.ts(1,10): error TS2614: Module '"./math.js"' has no exported member 'add'. Did you mean to use 'import add from "./math.js"' instead?

Following the suggestion and removing the braces, as in import add from "./math.js", made the exit code 0. Running the code printed 3. The MDN explanation of import also notes that a named import and a default import are different syntax.

Importing from an npm package

The same error occurs with packages. The type definitions (index.d.ts) of the ID generator nanoid 5.1.16 export nanoid, customAlphabet, customRandom, urlAlphabet, and random. Importing uuid, which is not in that list, produced TS2305.

ts
import { uuid } from "nanoid";

console.log(uuid());
console
$ npx tsc --noEmit
src/index.ts(1,10): error TS2305: Module '"nanoid"' has no exported member 'uuid'.

For packages, the quoted part of the message shows the package name instead of a file path. When I misspelled the name as import { nanoId } from "nanoid", TS2724 suggested Did you mean 'nanoid'?.

To check what nanoid exports, I looked at the export lines in node_modules/nanoid/index.d.ts. After I changed the import to import { nanoid } from "nanoid", npx tsc --noEmit exited with code 0. Running the code printed 21, the length of the generated ID.

When the re-exporting file is missing a name

I placed the functions in src/utils/math.ts, re-exported them from src/utils/index.ts, and imported from ./utils/index.js in other files. math.ts exports both add and subtract. However, index.ts only re-exports add.

ts
// src/utils/index.ts
export { add } from "./math.js";
ts
// src/index.ts
import { add, subtract } from "./utils/index.js";

console.log(add(1, 2), subtract(3, 1));

Running npx tsc --noEmit in this state produced TS2305.

console
$ npx tsc --noEmit
src/index.ts(1,15): error TS2305: Module '"./utils/index.js"' has no exported member 'subtract'.

The message names the ./utils/index.js from the import's from clause, not math.ts, where the function is written. Opening math.ts and confirming that subtract is there does not clear the error.

Point

The module named in a TS2305 message is the one in the import's from clause. Even if the function lives in another file, the error occurs when the file named in from does not export that name.

Changing index.ts to export { add, subtract } from "./math.js"; made the exit code 0. Re-exporting with export * from "./math.js";, which re-exports all named exports of math.ts, also gave exit code 0, and running the code printed 3 2.

Message differences across TypeScript versions

I put together the files that produce TS2305, TS2724, and TS2459 and ran npx tsc --noEmit again with TypeScript 5.9.3 and 6.0.3. The four errors had the same codes, text, and positions in every version.

console
$ npx tsc --noEmit
src/a.ts(1,15): error TS2305: Module '"./math.js"' has no exported member 'multiply'.
src/b.ts(1,10): error TS2724: '"./math.js"' has no exported member named 'Add'. Did you mean 'add'?
src/c.ts(1,10): error TS2459: Module '"./math.js"' declares 'subtract' locally, but it is not exported.
src/d.ts(1,10): error TS2305: Module '"nanoid"' has no exported member 'uuid'.

The only difference was the exit code of tsc: 2 for 5.9.3 and 6.0.3, and 1 for 7.0.2. When there were no errors, every version exited with code 0. If CI checks whether type checking passed, judging by whether the exit code is 0 avoids depending on this version difference.

​Posted by 86