Nuxt.js前端的Docker合成设置中出现ERR_REQUIRE_ESM错误

hs1ihplo  于 2023-01-20  发布在  Docker
关注(0)|答案(1)|浏览(130)

所以我有一个前端Nuxt.js和一个FastAPI都在一个Docker容器中。两个服务器运行命令都可以在没有Docker的情况下工作。
Docker的文件系统如下所示:

├── Dockerfile -- FastAPI docker file
├── docker-compose.yml 
└── front
    ├── Dockerfile -- Nuxt docker file

运行docker-compose up时,FastAPI服务器正常工作,但nuxt抛出此错误

Nuxt-frontend  | > @ start /front
Nuxt-frontend  | > nuxt start
Nuxt-frontend  |
Nuxt-frontend  | internal/modules/cjs/loader.js:975
Nuxt-frontend  |     throw new ERR_REQUIRE_ESM(filename);
Nuxt-frontend  |     ^
Nuxt-frontend  |
Nuxt-frontend  | Error [ERR_REQUIRE_ESM]: Must use import to load ES Module: /front/node_modules/nuxt/bin/nuxt.mjs
Nuxt-frontend  |     at Module.load (internal/modules/cjs/loader.js:975:11)
Nuxt-frontend  |     at Function.Module._load (internal/modules/cjs/loader.js:877:14)
Nuxt-frontend  |     at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:74:12)
Nuxt-frontend  |     at internal/main/run_main_module.js:18:47 {
Nuxt-frontend  |   code: 'ERR_REQUIRE_ESM'
Nuxt-frontend  | }
Nuxt-frontend  | npm ERR! code ELIFECYCLE
Nuxt-frontend  | npm ERR! errno 1
Nuxt-frontend  | npm ERR! @ start: `nuxt start`
Nuxt-frontend  | npm ERR! Exit status 1
Nuxt-frontend  | npm ERR!
Nuxt-frontend  | npm ERR! Failed at the @ start script.
Nuxt-frontend  | npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
Nuxt-frontend  |
Nuxt-frontend  | npm ERR! A complete log of this run can be found in:
Nuxt-frontend  | npm ERR!     /root/.npm/_logs/2023-01-07T06_09_00_616Z-debug.log

Dockerfile看起来像这样:

FROM node:12.16.3-alpine3.9

# Create destination
RUN mkdir -p /front
WORKDIR /front

#compile app
COPY package.json .
COPY package-lock.json .

RUN npm install

COPY . .
RUN npm build

EXPOSE 3000

ENV NUXT_HOST=0.0.0.0
ENV NUXT_PORT=3000

CMD [ "npm","run", "start" ]

.dockerignore看起来像这样:

node_modules
npm-debug*
.nuxt
dist
tests
.output

docker-compose.yml看起来像这样:

version: '2'
services:
    app:
        build: .
        container_name: FastAPI
        command: uvicorn app.main:app --host 0.0.0.0
        ports:
            - 8000:8000
    front:
        build: ./front
        container_name: Nuxt-frontend
        command: npm start
        ports:
            - 3000:3000

注意:我已经尝试过将"type": "module"添加到package.json中

rqqzpn5f

rqqzpn5f1#

不知道原因,但这是我如何解决它。
进入Docker桌面并删除所有容器和图像。
将docker文件更改为:

FROM node:17-alpine

RUN mkdir -p /front
WORKDIR /front
COPY . .

RUN npm ci
RUN npm cache clean --force
RUN npm run build

ENV NUXT_HOST=0.0.0.0
ENV NUXT_PORT=3000

EXPOSE 3000

CMD [ "node", "/front/server/index.mjs"]

docker-compose.yaml看起来像这样:

front:
    build: ./front
    container_name: Nuxt-frontend
    volumes:
        - /front/node_modules
    command: npm run dev
    ports:
        - 3000:3000

然后运行docker compose up

相关问题