【问题标题】:Monorepo avoid (Cannot find module 'lib') when import a class from other project file从其他项目文件导入类时,Monorepo 避免(找不到模块“lib”)
【发布时间】:2021-01-22 06:26:52
【问题描述】:

我在 simple monorepo 的 /frontend 文件夹中使用 /backend 文件夹中的类型/接口/类,但我的前端没有有一些后端库 有一种方法可以导入使用某些库的文件中定义的类而不会出现错误找不到模块'libname',而无需创建第三个文件或共享文件夹 不导入任何库?

当前文件夹结构示例

/root

  /backend
    /models/alternative.model.ts

  /frontend
    /components/frontend.component.ts

示例后端文件 alternative.model.ts

import { ObjectType } from '@nestjs/graphql';
import { BaseModel } from '../../common/models/base.model';
import { Exercise } from '../exercise/exercise.model';

@ObjectType()
export class Alternative extends BaseModel {
  text: string;
  isAnswer: boolean;
  exerciseId: string;
  exercise: Exercise;
}

前端导入示例frontend.component.ts

import { Alternative } from '../../../../../../backend/src/cases/alternative/alternative.model'

build

中的示例导入错误
../backend/src/cases/alternative/alternative.model.ts:1:46
Type error: Cannot find module '@nestjs/graphql' or its corresponding type declarations.

跨项目monorepo共享类文件

【问题讨论】:

  • 如果您为前端 repo 安装了 @nestjs/graphql 节点模块,它对您有用吗?
  • 不,但如果可行,这不是一个好选择:/

标签: typescript nestjs monorepo


【解决方案1】:

我的解决方法是在尝试构建前端之前在后端安装所有依赖项

我使用的是 docker,这是我的示例 dockerfile(nextjs 前端)

FROM node:lts-alpine AS deps
WORKDIR /app
COPY . .
WORKDIR ./backend
RUN npm install
WORKDIR ../frontend
RUN yarn install --frozen-lockfile

FROM node:lts-alpine AS builder
ENV NODE_ENV=production
WORKDIR /app
COPY . .
WORKDIR ./backend
COPY --from=deps /app/backend/node_modules ./node_modules
WORKDIR ../frontend
COPY --from=deps /app/frontend/node_modules ./node_modules
RUN yarn build

FROM node:lts-alpine AS runner
WORKDIR /app
ENV NODE_ENV=production
COPY --from=builder /app/frontend/next.config.js ./
# COPY --from=builder /app/frontend/public ./public
COPY --from=builder /app/frontend/.next ./.next
COPY --from=builder /app/frontend/node_modules ./node_modules
CMD ["node_modules/.bin/next", "start"]

【讨论】:

    猜你喜欢
    • 2020-01-28
    • 2020-08-01
    • 1970-01-01
    • 2021-08-22
    • 2022-10-20
    • 2016-05-29
    • 2021-03-17
    • 1970-01-01
    • 2019-11-19
    相关资源
    最近更新 更多