-1

I have followed this tutorial and it works well but when I try to dockerize, the build is ok but when I run the image an error appears : Error: Cannot find module 'express'

DockerFile

    FROM node:12 as buildContainer
    WORKDIR /app
    COPY ./package.json ./package-lock.json /app/
    RUN npm install
    COPY . /app
    # max-old-space is needed to avoid any compilation issues because of missing memory
    ENV NODE_OPTIONS --max-old-space-size=2048
    RUN npm run build:ssr

    FROM node:12-alpine

    WORKDIR /app
    COPY --from=buildContainer /app/package.json /app

    # Get all the code needed to run the app
    COPY --from=buildContainer /app/dist /app/dist

    EXPOSE 4000

    ENV NODE_ENV production
    CMD ["npm", "run", "serve:ssr"]

My dist structure is :

dist\app

  • browser -- en -- fr
  • server -- en -- fr proxy-server.js

proxy-server.js

    const express = require("express");
    const path = require("path");

    const getTranslatedServer = (lang) => {
      const distFolder = path.join(
        process.cwd(),
        `dist/morefont-v4/server/${lang}`
      );
      const server = require(`${distFolder}/main.js`);
      return server.app(lang);
    };

    function run() {
      const port = 4000;

      // Start up the Node server
      const appFr = getTranslatedServer("fr");
      const appEn = getTranslatedServer("en");

      const server = express();
      server.use("/fr", appFr);
      server.use("/en", appEn);
      server.use("", appEn);

      server.listen(port, () => {
        console.log(`Node Express server listening on http://localhost:${port}`);
      });
    }

    run();

Optional question : Do you have a proper way to use I18n with Angular SSR ?

Question : How can I fix my Docker image in order to importe node_modules ?

Thank you

double-beep
  • 5,031
  • 17
  • 33
  • 41
mbagiella
  • 256
  • 2
  • 12

1 Answers1

1

When you start a new build stage with a FROM line, the build sequence essentially starts over completely; nothing will be in the final image unless you explicitly COPY it there or RUN an install step.

With a typical "build in the first stage, run in the second stage" setup, you can skip installing the development dependencies in the final stage. That would suggest re-running npm install, after you set NODE_ENV=production:

FROM node:12-alpine
WORKDIR /app
COPY --from=buildContainer /app/package.json .

# Install dependencies (but not devDependencies)
ENV NODE_ENV production  # move this up from the bottom
RUN npm install          # add this

COPY --from=buildContainer /app/dist ./dist
EXPOSE 4000
CMD ["npm", "run", "serve:ssr"]
David Maze
  • 130,717
  • 29
  • 175
  • 215
  • It works ! However, instead of RUN npm install, I'm using RUN npm express. It reduces the docker image size significantly – mbagiella Jan 17 '21 at 12:50