0

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
Ndor
  • 19
  • 7

1 Answers1

1

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/*"]
    }
Kaneki21
  • 1,323
  • 2
  • 4
  • 22
  • hey, same error – Ndor Aug 10 '22 at 19:14
  • Edited the answer, please try that – Kaneki21 Aug 10 '22 at 19:17
  • I check your answer but somehow its still dont work :( I also saw this post you mantion before and saw this solution but i still get this error – Ndor Aug 10 '22 at 19:27
  • 1
    Try the new edit. In VS code : CTRL + SHIFT + P then type reload or Reload Project might also help – Kaneki21 Aug 10 '22 at 19:34
  • man still the same error, bruo i think my project is broken somehow :( , i dont know what to do , some of your solution suppose to work. But nothing change i am so mad – Ndor Aug 10 '22 at 21:09