Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

typescript error TS2305: Module "constants" has no exported member

I have Vite + React + TypeScript app with structure

src
  constants
    a.ts
    b.ts
    index.ts
  components
    Comp.tsx

tsconfig with "baseUrl": "src"

a.ts content:

export const ARRAY = [1, 2, 3];

b.ts content:

export const Object = { foo: 'bar' };

index.ts content:

export * from './a';
export * from './b';

Comp.tsx content:

import { ARRAY } from 'constants';

App works correctly, but in the WebStorm there is an error in Comp.tsx:

TS2305: Module  "constants"  has no exported member  ARRAY 

Why? How can I fix it?

I want to get rid of this error.

like image 486
Илья Avatar asked Oct 19 '25 13:10

Илья


1 Answers

The constants itself is a reserved word for the node types lib (Usually reside in this path: node_modules/@types/node/constants.d.ts). Therefore, it can be conflicted if you do not explicitly define the proper path to the constants directory in your project configs.

How to fix it?

You have two ways to fix this issue:

  1. The best and easiest way to solve this issue is to avoid using constants as a direct directory in your project. You can either move it under the utils directory (It mostly depends on your project design and architecture), or you can rename it to other custom names such as consts, localConstants, etc.

  2. Explicitly define the exact path for your project's constants directory within the compilerOptions settings. In your case, you can define it in tsconfig.* (JS-based projects can be configured in the jsconfig.*).

    According to the documentation you can do this like below:

    {
      "compilerOptions": {
        "paths": {
           "constants": ["./src/constants"]
        }
      }
    }
    
like image 85
SMAKSS Avatar answered Oct 22 '25 03:10

SMAKSS



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!