skip to Main Content

I am currently learning Docker and trying to follow this course (https://devopswithdocker.com/part-1/section-6/) to develop my skills.
Although I was able to do everything until now, I am stuck on this exercise (1.12 down the page):

I have made a Dockerfile to containerize the project (exampel-frontend) by following the instructions from the read-me file ( https://github.com/docker-hy/material-applications/blob/main/example-frontend/README.md) with the following code (actually I have written almost similar but this is the solution that can be found here: https://github.com/oneiromancy/devops-with-docker

FROM ubuntu:latest

WORKDIR /usr/src

COPY . .

RUN apt-get update && apt-get install -y curl && curl https://deb.nodesource.com/setup_14.x | apt-get install -y nodejs

RUN apt-get install -y npm && npm install && npm run build && npm install -g serve

CMD ["npx", "serve", "-s", "-l", "8080", "build"]

EXPOSE 8080

However, when I build and run the container :


docker build . -t hello-frontend
docker run -p 8080:8080 hello-frontend

I get the following error:

file:///usr/local/lib/node_modules/serve/build/main.js:169
      const ipAddress = request.socket.remoteAddress?.replace("::ffff:", "") ?? "unknown";
                                                     ^

SyntaxError: Unexpected token '.'
    at Loader.moduleStrategy (internal/modules/esm/translators.js:133:18)
    at async link (internal/modules/esm/module_job.js:42:21)

Could you help me to fix it as I need this application to be running for the following exercises …

Thank you!

3

Answers


  1. Chosen as BEST ANSWER

    Thank you all for your answers! The final version that helped me solve all the errors was :

    FROM node:16  
    WORKDIR /usr/src/app  
    
    COPY package*.json ./  
    
    RUN npm install 
    
    COPY . . 
    
    EXPOSE 8080 
    
    RUN npm list 
    RUN npm run build && npm install -g serve  
    
    CMD ["npx", "serve", "-s", "-l", "8080", "build"]
    
    

    The problem I was facing was coming mostly from the fact that COPY package*.json ./ was missing in my Dockerfile.

    I found out about it by adding : RUN npm list

    In case anyone has the same problem ...


  2. Your node version is less than Node 12.22.9.

    syntax (?.)

    That feature was introduced in Node 14.

    You should upgrade your version of Node. The current version of Node v19, but Active LTS version is v18 version https://nodejs.org/en

    This link is helpful for update node version How to install a specific version of Node on Ubuntu?

    Login or Signup to reply.
  3. Instead of basing your image on ubuntu:latest and installing Node 14.x yourself (which reaches end of life in May), you might want to consider switching to basing it on node:lts which is an image that already has the latest long-term-service Node version installed.

    Then you can cut your Dockerfile down to

    FROM node:lts
    
    WORKDIR /usr/src
    
    COPY . .
    
    RUN npm install && npm run build && npm install -g serve
    
    CMD ["npx", "serve", "-s", "-l", "8080", "build"]
    
    EXPOSE 8080
    

    Currently, the lts version of Node is version 18. If you, for some reason, need to run Node 14, you can use the node:14 image instead.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search