admin管理员组文章数量:1308604
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, {})
}
}
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
Share
Improve this question
edited Mar 14, 2022 at 8:18
John
asked Mar 13, 2022 at 19:31
JohnJohn
1751 gold badge3 silver badges9 bronze badges
1
- Please do not post pictures of code or error messages etc. Read e.g. this answer on Meta 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 article and edit your question. – Emma Commented Mar 13, 2022 at 19:43
1 Answer
Reset to default 7Fastest 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.
"pilerOptions": {
"baseUrl": "src",
"paths": {
"src/*": ["src/*"],
"entities/*": ["src/entities/*"],
"resolvers/*": ["src/resolvers/*"]
...
},
...
}
Or you could use a package like this one installed to your devDependecies
本文标签: javascriptError Cannot find module 39srcentitiesPost39Stack Overflow
版权声明:本文标题:javascript - Error: Cannot find module 'srcentitiesPost' - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741831743a2399973.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论