从其他项目文件导入 class 时,Monorepo 避免(找不到模块 'lib')
Monorepo avoid (Cannot find module 'lib') when import a class from other project file
我正在使用 /backend 文件夹中的 /frontend 文件夹中的 types/interfaces/classes simple monorepo,但我的前端没有一些后端库
有一种方法可以导入在使用某些库的文件中定义的 class 而不会出现错误 Cannot find module 'libname',而无需创建 third file 或 不导入任何库的共享文件夹?
当前文件夹结构示例
/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.
共享class文件跨项目monorepo
我的解决方法是在尝试构建前端之前在后端安装所有依赖项
我正在使用 docker,这是我的示例 docker文件(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"]
我正在使用 /backend 文件夹中的 /frontend 文件夹中的 types/interfaces/classes simple monorepo,但我的前端没有一些后端库 有一种方法可以导入在使用某些库的文件中定义的 class 而不会出现错误 Cannot find module 'libname',而无需创建 third file 或 不导入任何库的共享文件夹?
当前文件夹结构示例
/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.
共享class文件跨项目monorepo
我的解决方法是在尝试构建前端之前在后端安装所有依赖项
我正在使用 docker,这是我的示例 docker文件(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"]