Ndor
Ndor

Reputation: 29

How to fix tsconfig.json module not found?

I have the following folder structure:

\---src  
|   \---test
|   |   \---login
|   |       index.ts
|   \---utils
|       |   utilsFuntion.ts

i want to import the utilsFuntion from utils filder under src inside the login folder like this way: import {somefuntion} from 'utils/utilsFuntion';

instead of doing like this : import {somefuntion} from '../../../utils/utilsFuntion';

here is my tsconfig file:

{
    "compilerOptions": {
        "target": "es6",
        "lib": ["dom", "dom.iterable", "esnext"],
        "strict": true,
        "module": "commonjs",
        "noEmit": true,
        "resolveJsonModule": true,
        "esModuleInterop": true,
        "baseUrl": "src",
        "paths": {
          "src/*": ["src/*"]
        }
    },
    "include": ["src"]
}

after i add the src baseUrl and the paths i change the import like this: import {somefuntion} from 'utils/utilsFuntion'; i got error that say: Error: Cannot find module 'utils/helpFunction

any idea how to fix this?

i try:

./utils/utilsFuntion.ts
./utils/utilsFuntion
utils/utilsFuntion.ts
src/utils/utilsFuntion.ts
src/utils/utilsFuntion

Upvotes: 1

Views: 2541

Answers (1)

Kaneki21
Kaneki21

Reputation: 1420

You can refer this using absolute paths in typescript for imports

Try this

import {somefuntion} from '@src/utils/utilsFuntion';

In the tsconfig

"paths": {
      "@src/*": ["./src/*"]
    }

Upvotes: 1

Related Questions