martpie / next-transpile-modules

Next.js plugin to transpile code from node_modules. Please see: https://github.com/martpie/next-transpile-modules/issues/291
MIT License
1.13k stars 85 forks source link

An error occurred when "next build" with docker #258

Closed yuuk closed 1 year ago

yuuk commented 2 years ago

Are you trying to transpile a local package or an npm package? local package

Describe the bug I've clone the next monorepo demo https://github.com/martpie/monorepo-typescript-next-the-sane-way in my local machine(windows 10). Then I run next build, everything is ok!

Snipaste_2022-02-25_20-33-14

But when I do the same thing with docker, there was an error. Snipaste_2022-02-25_20-35-29

The dockerfile config:

FROM node:12.22.0-alpine as builder
WORKDIR /
COPY package.json .
COPY yarn.lock .
RUN yarn install
COPY . .
EXPOSE 80
RUN yarn build:app

The root package.json:

{
  "name": "monorepo-typescript-next-the-sane-way",
  "private": true,
  "workspaces": [
    "shared",
    "website"
  ],
  "scripts": {
    "build:app": "yarn --cwd=website build"  // Just and the script for dockerfile
  }
}

To Reproduce Here is a repo i've created to reproduce the problem. https://github.com/yuuk/monorepo-typescript-next-the-sane-way-with-docker

Expected behavior Transpile modules without errors

Setup

martpie commented 2 years ago

Hi there!

What you usually want to do with Docker is not to build your app with Docker unless you use multi-layer images. Otherwise you may end up with dev artifacts in your final docker image (node_modules, dev deps, not transpiled source code, etc).

My main advice is to build your app on your CI, then copy the Next.js build to your docker image. I will try to find a working Dockerfile if I can find an old professional project.

gndelia commented 2 years ago

Having the same issue here, building in docker throws an error about not finding a local package that's installed as a dependency

ErlanBelekov commented 2 years ago

+1

martpie commented 2 years ago

Have you applied what I recommended here in my message above?

What you usually want to do with Docker is not to build your app with Docker unless you use multi-layer images. Otherwise you may end up with dev artifacts in your final docker image (node_modules, dev deps, not transpiled source code, etc).

andrussik commented 2 years ago

Seems like symlinks are unstable with docker. I had strange situation where it worked locally, but got same error in pipeline during deployment.

You can fix it by copying your directory to node_modules before build: COPY shared node_modules/shared RUN yarn build:app

martpie commented 2 years ago

I would definitely recommend installing your deps before then copying them, rather than installing them from your Dockerfile.

Yes, it is less "the Docker way", but you'll save yourselves the hundreds of hours I spent trying to make this work.

thara-3201 commented 2 years ago

is there any solution to it?

I'm facing an increase in deployment time significantly after using this module, has anyone faced this issue too?

martpie commented 2 years ago

is there any solution to it?

I'm facing an increase in deployment time significantly after using this module, has anyone faced this issue too?

Where are you building your app? From your CI or from Docker? If Docker, try running the build on CI instead.