添加链接
link之家
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
相关文章推荐
刚毅的紫菜  ·  用于Databricks ...·  1 年前    · 
礼貌的小熊猫  ·  Asp.net ...·  1 年前    · 
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

I have hit an issue while building a docker image for a NestJS microservice.

When I run npm start locally, this is response I see:

11:16:05 am - Starting compilation in watch mode...
11:16:12 am - Found 0 errors. Watching for file changes.
[Nest] 32132   - 16/02/2022, 11:16:15 am   [NestFactory] Starting Nest application...     
[Nest] 32132   - 16/02/2022, 11:16:16 am   [InstanceLoader] ConfigHostModule dependencies 
initialized +363ms
[Nest] 32132   - 16/02/2022, 11:16:16 am   [InstanceLoader] AppModule dependencies initialized +2ms
[Nest] 32132   - 16/02/2022, 11:16:16 am   [InstanceLoader] ConfigModule dependencies initialized +1ms
[Nest] 32132   - 16/02/2022, 11:16:17 am   [InstanceLoader] DatabaseModule dependencies initialized +1655ms
[Nest] 32132   - 16/02/2022, 11:16:17 am   [NestMicroservice] Nest microservice successfully started +15ms

However when I run the docker image, this is the response I get:

5:41:10 AM - Starting compilation in watch mode...
node_modules/sequelize-typescript/dist/hooks/shared/hook-meta.d.ts(2,32): error TS2307: Cannot find module 'sequelize/types/lib/hooks' or its corresponding type declarations.
node_modules/sequelize-typescript/dist/hooks/shared/hooks-service.d.ts(4,32): error TS2307: Cannot find module 'sequelize/types/lib/hooks' or its corresponding type declarations.
5:41:34 AM - Found 2 errors. Watching for file changes.

My Docker file looks like this. I am pre-building the application with npm start and copying over all folders to a content-service folder created in docker root. I have excluded the node_modules and package-lock.json through .dockerignore

FROM node:14.16-alpine
WORKDIR /content-service
COPY . . 
RUN npm install
RUN npm i @nestjs/cli
RUN npm install --save sequelize-cli
RUN npm i -g pm2
EXPOSE 3002
CMD ["npm","start"]

This is how my local directory looks. I can see the hooks.d.ts file here under sequelize/types/lib Local directory screenshot

But when I look inside the container running this image, I dont see this folder. The hook.d.ts file is placed directly inside of sequelize/types instead of sequelize/types/lib:

Container sreenshot

When I directly update the hooks-service.d.ts and hook-meta.d.ts files inside the container to point towards sequelize/types instead of sequelize/types/lib, the application starts.

6:09:20 AM - Found 0 errors. Watching for file changes.
[Nest] 446   - 02/16/2022, 6:09:27 AM   [NestFactory] Starting Nest application...

Any pointers on how I can build it without this would be greatly appreciated.

My local machine versions:

  • Angular CLI: 13.1.2 Node: 16.13.1 Package Manager: npm 6.14.7 OS: win32 x64
  • Edit: Resolved thanks to a suggestion from @DavidMaze. I was ignoring package-lock.json. Copying 'Package-lock.json' into the image fixed this issue for me!

    Please check comments on this answer for the solution.

    If you map a volume to /content-service when you run it, then that's the reason. That hides the content in the image and replaces it with the directory you map into it. – Hans Kilian Feb 16, 2022 at 8:04 @HansKilian Thanks for your response. Do you mind expanding it further? I actually created /content-service folder directly on the docker container root since moving it to another folder like usr or var threw some issues. – Vijay Feb 16, 2022 at 9:31 You should copy the package-lock.json into your container (before you RUN npm install); that ensures you'll have the exact same package versions in the development and container environments. How are you running the container? Can you do something like RUN npm build in the Dockerfile to avoid repeating the compilation at every container startup? – David Maze Feb 16, 2022 at 11:39 @DavidMaze Hi David. I am running the container with Docker run -p 3000:80 <image> just to test it out. I did try to do a 'RUN npm build' inside the image, but I hit issues with sequelize there too, which is why right now I am pre-building the app offline and copying /DIST into the image. Also, I will take your input and try with package-lock,json – Vijay Feb 16, 2022 at 12:12 @DavidMaze Thanks a ton! Copying package-lock.json worked! I have hit another error due to elastic search. But I have crossed this error. – Vijay Feb 16, 2022 at 13:15

    Thanks for contributing an answer to Stack Overflow!

    • Please be sure to answer the question. Provide details and share your research!

    But avoid

    • Asking for help, clarification, or responding to other answers.
    • Making statements based on opinion; back them up with references or personal experience.

    To learn more, see our tips on writing great answers.