Namespace has no exported member or Module has no exported member with circular dependencies in TypeScript? – Typescript

by
Ali Hasan
es6-modules react-typescript

Quick Fix: Verify that the exported member is indeed exported in the corresponding module. If it is, then you can try to fix the issue by adding the following line to the top of the file causing the error:

export { [member] } from './[path]';

The Problem:

A TypeScript project with circular dependencies encounters an error where some imports fail with the message "Module ‘"~"’ has no exported member ‘"Foo"’", while others succeed. Specifically, importing specific named exports using curly braces fails, while importing the entire namespace using an asterisk succeeds for some but not all types.

The Solutions:

Solution 1: Organize your code

In the provided code, the issue arises due to a mismatch between the exported members from the code.js file and how it’s being imported and used in other files. To resolve this, you should ensure that the exported members match the expected imports.

Here’s an example of how you can organize your code to avoid such issues:

In index.js:

import * as code from './code/index.js';
export default code;

In code/index.js:

export { Foo, Bar };

In other files where you need to import specific members:

import { Foo, Bar } from '~';

In other files where you need to import all members:

import * as code from '~';

This way, you can ensure that the exported members are available and can be imported as expected, avoiding errors related to missing or undefined exported members.

Q&A

What is the difference between Model and Namespace has no exported member with circular dependencies?

Namespace error is normally associated with exported member issue whereas Model error is associated with imported member issue

Is importing/exporting as * allowed in Typescript?

Importing & exporting as * is not allowed in TS, only specific named members can be imported/exported

Can I fix circular dependency issues in TS?

Yes, circular dependencies can be resolved by using barrel files