如何在NX中从库中导入

huangapple go评论66阅读模式
英文:

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

huangapple
  • 本文由 发表于 2023年7月3日 19:18:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/76604224.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定