您在其他平台上安装了 esbuild,而不是您当前使用的平台。

22

我正在尝试将 Svelte js 应用程序放入 Docker 容器中,但是日志中出现了关于 esbuild 在不同平台上的错误。我正在使用 M1 Mac,我尝试安装 esbuild-wasm,如日志所建议,并在 Docker 文件中尝试了 npm i esbuild-linux-arm64 作为步骤,并尝试了 RUN npm install yarn,因为日志建议使用内置工具处理平台,但它没有起作用。 我的 Docker 文件:

FROM node:16.10.0
WORKDIR /my-website
COPY package.json .
RUN npm install
COPY . .
EXPOSE 3000
CMD ["npm", "start"]

错误在于

You installed esbuild on another platform than the one you're currently using.
This won't work because esbuild is written with native code and needs to
install a platform-specific binary executable.

Specifically the "esbuild-darwin-arm64" package is present but this platform
needs the "esbuild-linux-arm64" package instead. People often get into this
situation by installing esbuild on Windows or macOS and copying "node_modules"
into a Docker image that runs Linux, or by copying "node_modules" between
Windows and WSL environments.

实际上在运行 esbuild 的是什么(您是否在 Dockerfile 中缺少 RUN npm run build 行)?您是否在 .dockerignore 文件中排除了主机的 node_modules 目录?您是在构建镜像或运行容器时遇到此错误,如果正在运行,则是否使用 volumes: 替换了镜像内容? - David Maze
当我运行docker-compose up --build时,我遇到了错误。 我的docker-compose文件:version: '3.8' services: rad-website: build: context: . dockerfile: Dockerfile ports: - "3000:3000" volumes: - .:/my-website - /my-website/node_modules/ - Yusuf
7个回答

29

您已经将 node_modules 从本地环境复制到容器中。 本地您拥有适用于 darwin-arm64 架构的软件包,但是在容器内部,它是一个需要适用于 linux-arm64 的 Linux 系统。

为了避免此类错误,您不应将 node_modules 复制到容器中。

您只需要将 node_modules 添加到 .dockerignore 文件即可。


1
糟糕!我也不敢相信我犯了这个错误...谷歌把我带到了这里。 - Spock

7
我删除了 node_modules 并运行 npm install 重新安装 node_modules,问题得到解决。

你好,首先感谢你抽出时间来回答,虽然, 正确的答案是缺少 .dockerignore 的那个,而不是 node_modules。 另外,如果你添加代码片段,你的答案将会更加突出,并且你会获得更多的声望。 - Ion Utale

3

我在我的 React 应用程序中遇到了同样的问题...但是我在使用 Docker Compose,并且尽管我将 .dockerignore 文件添加到我的设置中,但它并没有起作用。

我有一个与自动重新加载更改相关联的卷...

所以,如果你有同样的问题... 确保你通过有选择地选择要挂载的文件来排除 "node_modules" 目录。

根目录结构

project/
    |-- docker-compose.yaml
    |-- frontend/
        |--Dockerfile
        |--README.md
        |--index.html
        |--node_modules
        |--package-lock.json
        |--package.json
        |--public
        |--src
        |--vite.config.js
    |-- backend/
        |--Dockerfile
        |--server/
    

组合文件

version: "3.9"

services:
  frontend:
    build:
      context: ./frontend
      dockerfile: Dockerfile
    container_name: bgt_frontend
    ports:
      - "5000:5000"
    volumes:
    # to make changes dynamically without reloading docker compose
    # this part caused the issue... 
    # since i had my node_modules in the frontend directory 
    # so it was also mounted to the container..
      - ./frontend:/app

修复:
version: "3.9"

services:
  frontend:
    build:
      context: ./frontend
      dockerfile: Dockerfile
    container_name: bgt_frontend
    ports:
      - "5000:5000"
    volumes:
    # to make changes dynamically without reloading docker compose
    # add the directories or files one by one such that "node_modules"
    # is ignored via .dockerignore.
      - ./frontend/src:/app/src
      - ./frontend/index.html:/app/index.html

上述逻辑对我有效..希望在你遇到时也能对你有效..


1
对我来说很有效 :) 谢谢你提供的带有清晰评论的之前和之后的文件。 - undefined

2

1
当我把我的代码转移到新的电脑上时,我也遇到了一个非常相似的错误。我所需要做的只是运行npm install,这样就修复了一切。

0
我的问题(使用m1 Macbook Pro)是我的终端和Node版本都是x86(rosetta),而不是arm64。这对我有用:
  1. 确保你的终端使用arm,右键点击终端应用程序,获取信息并取消勾选rosetta。在终端中输入"arch"进行验证,你应该看到"arm64"
  2. 通过输入"node -e 'console.log(process.arch)'"确保你的node也使用arm
  3. 删除你的node模块并重新安装。
感谢这个人:https://gist.github.com/LeZuse/bf838718ff2689c5fc035c5a6825a11c

0
我发现我的节点版本管理器默认使用的是x64二进制文件,所以我不得不重新安装并指定arm64。
例如: fnm install --arch=arm64 16

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接