Docker Node.js容器错误:尝试运行容器时出现意外的标记“.”。

huangapple go评论77阅读模式
英文:

Docker Node.js container error unexpected token '.' when trying to run the container

问题

目前我正在学习Docker,并尝试遵循这个课程(https://devopswithdocker.com/part-1/section-6/)来提升我的技能。
尽管我已经完成了到目前为止的一切,但我在这个练习(页面下方的1.12)卡住了:

我已经创建了一个Dockerfile来将项目(exampel-frontend)容器化,按照自述文件的说明(https://github.com/docker-hy/material-applications/blob/main/example-frontend/README.md)使用以下代码(实际上我写了几乎相似的代码,但这是可以在这里找到的解决方案: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

然而,当我构建和运行容器时:

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

我得到了以下错误:

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)

你能帮助我修复它吗?因为我需要这个应用程序在接下来的练习中运行...

谢谢!

英文:

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!

答案1

得分: 1

你的Node版本低于Node 12.22.9。

语法 (?.)

该功能在Node 14中引入。

你应该升级你的Node版本。当前版本为Node v19,但Active LTS版本是v18,版本链接https://nodejs.org/en

此链接有助于更新Node版本https://stackoverflow.com/questions/16898001/how-to-install-a-specific-version-of-node-on-ubuntu/45726068#45726068

英文:

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 https://stackoverflow.com/questions/16898001/how-to-install-a-specific-version-of-node-on-ubuntu/45726068#45726068

答案2

得分: 1

不必再以ubuntu:latest为基础,并手动安装14.x版本的Node(该版本将于五月结束支持),您可以考虑基于node:lts构建,这是一个已经包含了最新长期支持版本的Node的镜像。

然后,您可以简化您的Dockerfile 如下:

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

目前,Node的lts版本是18。如果出于某种原因您需要使用Node 14,您可以使用node:14镜像。

英文:

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.

答案3

得分: 1

感谢大家的回答!帮助我解决所有错误的最终版本是:

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"]

我面临的问题主要是因为我的 Dockerfile 中缺少了 COPY package*.json ./ 这一部分。

我通过添加以下内容发现了这一问题:RUN npm list

如果有人遇到相同的问题...

英文:

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 ...

huangapple
  • 本文由 发表于 2023年3月31日 16:10:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/75896245.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定