英文:
how to import from libs in NX
问题
我有一个使用Nestjs和Nx的微服务。
这是项目结构:
project
│
│
└───apps
│ │ gateway
│ │ auth
│
│
└───libs
└───common
现在我想在gateway
中从libs/common
中导入一些模块。
import { RMQ_SERVICES } from '@app/common/constants/rmq.constant';
但是它不起作用。
无法找到模块'@app/common/constants/rmq.constant'或其对应的类型声明.ts(2307)
我也尝试过这个:
import { RMQ_SERVICES } from 'libs/common/src/lib/constants/rmq.constant';
但它也不起作用。
项目不能通过相对或绝对路径导入,并且必须以npm范围开头。eslint
那么我该如何做呢?
英文:
I have Microservice with Nestjs and Nx
this is project structure
project
│
│
└───apps
│ │ gateway
│ │ auth
│
│
└───libs
└───common
now I want to import some module from libs/common
in gateway
import { RMQ_SERVICES } from '@app/common/constants/rmq.constant';
but it doesn't work
Cannot find module '@app/common/constants/rmq.constant' or its corresponding type declarations.ts(2307)
I have also tried this
import { RMQ_SERVICES } from 'libs/common/src/lib/constants/rmq.constant';
but it doesn't worked .
Projects cannot be imported by a relative or absolute path, and must begin with a npm scopeeslint
so how can I do it ?
答案1
得分: 1
如果这些是使用nx生成器创建的,那么你的tsconfig.base.json
应该包含正确的paths
以映射库。通常你的nx.json
会有一个npmScope
属性,它是你的@scope/
,然后根据你如何设置--importPath
选项,这应该定义了你需要使用的路径。否则,请检查你的基础tsconfig.base.json
,看看路径是什么。通常在我的Nx monorepos中,我会有类似@scope/server/kysely
或@scope/shared/types
这样的库,就像这个例子中的一样。
如果这些不是使用Nx生成器创建的,那么你需要自己将路径映射添加到你的tsconfig.base.json
中,并确保你的应用程序的tsconfig.json
已经更新以扩展基本配置。
英文:
If these were created with the nx generators then your tsconfig.base.json
should contain the proper paths
for mapping the libraries. Generally your nx.json
will have an npmScope
property that is your @scope/
and then depending on how you set up the --importPath
option that should define what path you need to use. Check your base tsconfig.base.json
to see what the path is otherwise. I usually have libraries like @scope/server/kysely
or @scope/shared/types
in my Nx monorepos, like this one here.
If these were not created with the Nx generators, then you'll need to add the path mapping to your tsconfig.base.json
yourself and make sure that the tsconfig.json
of your applications are updated to extend from the base config
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论