KevinEdry / nestjs-trpc

Providing native support for decorators and implement an opinionated approach that aligns with NestJS conventions.
https://NestJS-tRPC.io
MIT License
52 stars 4 forks source link

Error: 'Could not access root module file' when using container #26

Closed yar2001 closed 1 week ago

yar2001 commented 1 week ago

Hi, when I deploy my project in k8s, it throws the Could not access root module file error:

[32m[Nest] 1  - [39m10/16/2024, 9:06:49 PM [32m    LOG [RouterExplorer] Mapped {/internal/setFrontendFile, POST} route +0ms
/app/node_modules/nestjs-trpc/dist/generators/trpc.generator.js:92
            throw new Error('Could not access root module file.');
                  ^

Error: Could not access root module file.
    at TRPCGenerator.buildRootImportsMap (/app/node_modules/nestjs-trpc/dist/generators/trpc.generator.js:92:19)
    at TRPCGenerator.onModuleInit (/app/node_modules/nestjs-trpc/dist/generators/trpc.generator.js:26:42)
    at MapIterator.iteratee (/app/node_modules/@nestjs/core/hooks/on-module-init.hook.js:22:43)
    at MapIterator.next (/app/node_modules/iterare/lib/map.js:13:39)
    at IteratorWithOperators.next (/app/node_modules/iterare/lib/iterate.js:21:28)
    at Function.from (<anonymous>)
    at IteratorWithOperators.toArray (/app/node_modules/iterare/lib/iterate.js:180:22)
    at callOperator (/app/node_modules/@nestjs/core/hooks/on-module-init.hook.js:23:10)
    at callModuleInitHook (/app/node_modules/@nestjs/core/hooks/on-module-init.hook.js:43:23)
    at NestApplication.callInitHook (/app/node_modules/@nestjs/core/nest-application-context.js:224:50)

Node.js v22.9.0

What does root module file mean? Should I copy some files more than dist or node_modules in my container?

Here is the dockerfile:

FROM node:22-alpine as build

WORKDIR /app

COPY package.json .yarnrc.yml yarn.lock ./
COPY .yarn .yarn
RUN yarn install

COPY . .
RUN yarn build

FROM node:22-alpine

WORKDIR /app

COPY --from=build /app/node_modules ./node_modules
COPY --from=build /app/dist ./dist
COPY --from=build /app/package.json ./package.json

ENV NODE_ENV=production

CMD ["node", "./dist/main.js"]

I have already disabled the schema file generation:

TRPCModule.forRoot({
   autoSchemaFile: process.env.NODE_ENV === 'production' ? undefined : './src/generated/trpc',
   context: AuthContext,
}),

Thank you for your reading!

KevinEdry commented 1 week ago

Oh I know exactly whats the issue, let me push a quick fix for it. I've basically changed how we do the file resolution, and need to disable it when autoSchemaFile is disabled since in production you build the code without the typescript code next to it. Let me push a quick fix for it.

Edit: To answer your question, in order to generate the schema file on the fly the adapter needs to somehow get the original TS file to read it and infer types, import maps etc... To do this I have a method that basically searches for the root app.module.ts if you opted to generate the schema file, you can see how this works under the GeneratorModule.

KevinEdry commented 1 week ago

Should be fixed in v1.5.1: https://www.npmjs.com/package/nestjs-trpc I've tested it locally and it seems to have worked, but can you confirm on your end?

yar2001 commented 1 week ago

Yes, I pushed a new image and it worked.

Thanks a lot, that was awesome!