2

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 
4
  • ./utils/utilsFuntion try this Commented Aug 10, 2022 at 18:24
  • based on the error output you've posted, it seems that you're trying to import utils/helpFunction, but the actual file is utils/utilsFuntion.js. Double check spelling and i think you'll be fine =) Commented Aug 10, 2022 at 18:28
  • hey, same error Commented Aug 10, 2022 at 19:14
  • Question: why are you working with js files in a typescript project? In any case, a first step would be to add allowJs in your tsconfig.json Commented Aug 10, 2022 at 19:21

1 Answer 1

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/*"] } 
Sign up to request clarification or add additional context in comments.

5 Comments

hey, same error
Edited the answer, please try that
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
Try the new edit. In VS code : CTRL + SHIFT + P then type reload or Reload Project might also help
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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.