3

I'm trying to get a docker container from my sveltekit app. I'm using the node adapter in my svelte.config.js

import adapter from '@sveltejs/adapter-node'
adapter: adapter(),

and my Dockerfile looks like this.

FROM node:16-alpine

WORKDIR /app
COPY package*.json ./

RUN npm install
COPY . ./

RUN npm run build

EXPOSE 3000

CMD [ "node", "build" ]

I'm getting this error when starting up the container:

node:internal/errors:464
2022-03-02T09:51:03.597044200Z     ErrorCaptureStackTrace(err);
2022-03-02T09:51:03.597088700Z     ^
2022-03-02T09:51:03.597100400Z 
2022-03-02T09:51:03.597116300Z Error [ERR_MODULE_NOT_FOUND]: Cannot find module '/app/build/server/index.js' imported from /app/build/index.js
2022-03-02T09:51:03.597144200Z     at new NodeError (node:internal/errors:371:5)
2022-03-02T09:51:03.597193600Z     at finalizeResolution (node:internal/modules/esm/resolve:418:11)
2022-03-02T09:51:03.597270100Z     at moduleResolve (node:internal/modules/esm/resolve:981:10)
2022-03-02T09:51:03.597285100Z     at defaultResolve (node:internal/modules/esm/resolve:1078:11)
2022-03-02T09:51:03.597298100Z     at ESMLoader.resolve (node:internal/modules/esm/loader:530:30)
2022-03-02T09:51:03.597311100Z     at ESMLoader.getModuleJob (node:internal/modules/esm/loader:251:18)
2022-03-02T09:51:03.597324900Z     at ModuleWrap.<anonymous> (node:internal/modules/esm/module_job:79:40)
2022-03-02T09:51:03.597336000Z     at link (node:internal/modules/esm/module_job:78:36) {
2022-03-02T09:51:03.597345300Z   code: 'ERR_MODULE_NOT_FOUND'
2022-03-02T09:51:03.597359500Z }
3
  • Can you post the versions of @sveltejs/adapter-node and @sveltejs/kit that you have installed? You may be using incompatible versions. Commented Mar 2, 2022 at 22:16
  • Why the COPY . ./ after the RUN npm install? (Add "node_modules" to the .dockerignore) Commented Mar 5, 2022 at 14:00
  • Did you find any solution? I am facing with the same problem Commented Jun 7, 2022 at 10:23

1 Answer 1

2

You can build a temporary docker image and build your project inside that image first. After, you can create your final production docker image using the prior build image. So, dockerfile should be like the below.

FROM node:alpine as build
WORKDIR /my-project
COPY . .
RUN npm ci --development --silent

FROM node:alpine as prod
COPY ./package*.json ./
RUN npm ci --production --silent --ignore-scripts
COPY --from=build /my-project/build ./build
EXPOSE 3000
USER node
CMD ["node", "./build"]
Sign up to request clarification or add additional context in comments.

2 Comments

ERROR: failed to solve: failed to compute cache key: failed to calculate checksum of ref 199a5af5-02fd-413d-b7c3-60ad8fba0bc3::i7luw5r4ov4755v4gj6jb6kmo: "/code/build": not found
I get the above error if I try running your Dockerfile. Guess the build is never created or created in some other place.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.