6

so i am creating a graphQL server using type-graph and mikro-orm everything was fine till i got this error that says => Error: Cannot find module 'src/entities/Post' and that module exists as you can see in this picture: folder structure

and this is what the error looks like int the terminal: error in the terminal

by the way i am using script called watch: "tsc -w" to convert typescript into javascript.

this is a code example of my postResolver:

import { Post } from './src/entities/Post';
import { MyContext } from 'src/types';
import {Ctx, Query, Resolver} from 'type-graphql';

@Resolver()
export class postResolver {
    @Query(()=> [Post])
    posts(@Ctx() {em}: MyContext) : Promise<Post[]>{
        return em.find(Post, {})
    }
}
it says that the module ./src/entities/Post does not exist while it exists and i really don't know why
John
  • 125
  • 2
  • 7
  • Please do not post pictures of code or error messages etc. Read e.g. [this answer on Meta](https://meta.stackoverflow.com/questions/285551/why-not-upload-images-of-code-errors-when-asking-a-question/285557#285557) for reasons why. You'll probably have a better chance of getting an answer if you also read the [_how do I ask a good question_](https://stackoverflow.com/help/how-to-ask) article and edit your question. – Emma Mar 13 '22 at 19:43

1 Answers1

5

Fastest way to solve it would be using relative imports (import { Post } from '../entities/Post') instead of absolute like you did.

Another way to satisfy node to keep using absolute paths, is adding the following to your tsconfig file. Note that you will need to add a path for every directory at 'src' level, you want to import.

"compilerOptions": {
  "baseUrl": "src",
  "paths": {
     "src/*": ["src/*"],
     "entities/*": ["src/entities/*"],
     "resolvers/*": ["src/resolvers/*"]
     ...
  },
  ...
}

Or you could use a package like this one installed to your devDependecies

Art
  • 431
  • 4
  • 12