sveltejs / kit

web development, streamlined

Home Page:https://kit.svelte.dev

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

adapter-node doesn't work correctly with segmented builds

Norlock opened this issue · comments

Describe the bug

When you segment the Dockerfile into multiple parts it won't run the node web server correctly. This is because the bundled files are referencing the node_modules outside the build directory.

If I run the following command:

head -c 1000 build/server/chunks/_page.svelte-B66aaGbY.js.map

I will get the following output:

{"version":3,"file":"_page.svelte-B66aaGbY.js","sources":["../../../node_modules/qrcode-generator/qrcode.js","../../../node_modules/dequal/dist/index.mjs","../../../node_modules/nanoid/non-secure/index.js","../../../node_modules/@floating-ui/utils/dist/floating-ui.utils.mjs","../../../node_modules/@floating-ui/core/dist/floating-ui.core.mjs","../../../node_modules/@floating-ui/utils/dist/floating-ui.utils.dom.mjs","../../../node_modules/@floating-ui/dom/dist/floating-ui.dom.mjs","../../../node_modules/tabbable/dist/index.esm.js","../../../node_modules/focus-trap/dist/focus-trap.esm.js","../../../node_modules/immutable-json-patch/lib/esm/typeguards.js","../../../node_modules/immutable-json-patch/lib/esm/utils.js","../../../node_modules/immutable-json-patch/lib/esm/immutabilityHelpers.js","../../../node_modules/immutable-json-patch/lib/esm/jsonPointer.js","../../../node_modules/immutable-json-patch/lib/esm/immutableJSONPatch.js","../../../node_modules/immutable-json-patch/lib/esm/revertJ⏎   

Then if I visit the web page it will return a 500 error.

Reproduction

You can toggle the following comments in the Dockerfile and see that the version that is not segmented is working but the segmented version is not:

#FROM node:22-bookworm AS builder
FROM node:22-bookworm

WORKDIR /app

COPY . .

# Clean install all node modules
RUN npm ci

# Build SvelteKit app
RUN npm run build

#FROM node:22-bookworm AS deployer

#WORKDIR /app

#COPY --from=builder /app/build build/
#COPY --from=builder /app/package.json .

EXPOSE 3000

ENV NODE_ENV=production

CMD [ "node", "build" ]

On the segmented version it will return a 500 error when trying to visit the web page.

Logs

No response

System Info

System:
    OS: Linux 6.9 Arch Linux
    CPU: (12) x64 AMD Ryzen 5 6600U with Radeon Graphics
    Memory: 10.57 GB / 14.81 GB
    Container: Yes
    Shell: 3.7.1 - /usr/bin/fish
  Binaries:
    Node: 22.3.0 - /usr/bin/node
    Yarn: 1.22.21 - /usr/bin/yarn
    npm: 10.8.1 - /usr/bin/npm
    pnpm: 8.12.0 - /usr/bin/pnpm
  Browsers:
    Brave Browser: 124.1.65.133
  npmPackages:
    @sveltejs/adapter-node: ^5.2.0 => 5.2.0 
    @sveltejs/kit: ^2.0.0 => 2.5.17 
    @sveltejs/vite-plugin-svelte: ^3.0.0 => 3.1.1 
    svelte: ^4.2.7 => 4.2.18 
    vite: ^5.1.7 => 5.3.2

Severity

annoyance

Additional Information

No response

We'll need a proper (minimal) reproduction. Sourcemaps referring to files that don't exist on the filesystem will not itself make anything fail.

svelte-problem.tar.gz
I have here the source code, I tried to make a barebone project but it works there. So here is the source code, it inside of a private repo so only the frontend is zipped.

The error I get:

Error [ERR_MODULE_NOT_FOUND]: Cannot find package 'tailwind-merge' imported from /app/build/server/chunks/_page.svelte-B66aaGbY.js
    at packageResolve (node:internal/modules/esm/resolve:841:9)
    at moduleResolve (node:internal/modules/esm/resolve:914:18)
    at defaultResolve (node:internal/modules/esm/resolve:1119:11)
    at ModuleLoader.defaultResolve (node:internal/modules/esm/loader:541:12)
    at ModuleLoader.resolve (node:internal/modules/esm/loader:510:25)
    at ModuleLoader.getModuleJob (node:internal/modules/esm/loader:240:38)
    at ModuleJob._link (node:internal/modules/esm/module_job:126:49) {
  code: 'ERR_MODULE_NOT_FOUND'
}

Prod dependencies in your package.json are runtime dependencies of your app: https://kit.svelte.dev/docs/adapter-node#deploying If you're not installing them in your final stage, they won't be there.

Ok but how should my Dockerfile look then, because I'm confused, the following file also doesn't work:

FROM node:22-bookworm AS builder
#FROM node:22-bookworm-slim

WORKDIR /app

COPY . .

# Clean install all node modules
RUN npm ci

# Build SvelteKit app
RUN npm run build

FROM node:22-bookworm AS deployer

WORKDIR /app

COPY --from=builder /app/build build/
COPY --from=builder /app/package.json .
COPY --from=builder /app/package-lock.json .
COPY --from=builder /app/node_modules .

# Clean install all node modules
RUN npm ci --omit dev

EXPOSE 3000

ENV NODE_ENV=production

CMD [ "node", "build" ]