Home > Mobile >  Docker compose build failed in my nuxt project because of axios module
Docker compose build failed in my nuxt project because of axios module

Time:12-07

I deployed a nuxt project with docker about six months ago, and it was working well, but now, I need to rebuild it, but I got this error:

> [builder 6/8] RUN yarn build:                                               
yarn run v1.22.19                                                         
$ nuxt build                                                              
Nuxi 3.0.0                                                                
Nuxt 3.0.0 with Nitro 1.0.0                                               

 ERROR  Cannot read properties of undefined (reading 'options')

  at axiosModule (node_modules/@nuxtjs/axios/lib/module.js:12:13)
  at installModule (node_modules/@nuxt/kit/dist/index.mjs:416:9)
  at async initNuxt (node_modules/nuxt/dist/index.mjs:1825:7)
  at async loadNuxt (node_modules/nuxt/dist/index.mjs:1857:5)
  at async loadNuxt (node_modules/@nuxt/kit/dist/index.mjs:493:19)
  at async Object.invoke (node_modules/nuxi/dist/chunks/build.mjs:34:18)
  at async _main (node_modules/nuxi/dist/cli.mjs:50:20)

error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.

I didn't change my code at all, and it was working before. I guessed it was about the node image version because it was node:lts-alpine, so I tried node:19-alpine3.15, node:19-alpine, node:19, node:18-alpine3.15, node:18-alpine, node:18, node:16-alpine, node:14-alpine, but none of them didn't solve my issue.

That error throws in the axios module, in node_modules/@nuxtjs/axios/lib/module.js:

function axiosModule (_moduleOptions) {
  const { nuxt } = this

  // Combine options
  const moduleOptions = {
    ...nuxt.options.axios,    # <---- Here, nuxt is undefined!!!
    ..._moduleOptions,
    ...(nuxt.options.runtimeConfig && nuxt.options.runtimeConfig.axios)
  }

I really don't know what's wrong with my project but I'll attach some files that I think might be helpful.

# package.json
{
  ...
  "dependencies": {
    "@nuxtjs/auth-next": "5.0.0-1648802546.c9880dc",
    "@nuxtjs/axios": "^5.13.6",
    "@nuxtjs/pwa": "^3.3.5",
    "core-js": "^3.19.3",
    "nuxt": "^2.15.8",
    "trading-vue-js": "^1.0.2",
    "tvjs-overlays": "^0.5.0",
    "vue": "^2.6.14",
    "vue-server-renderer": "^2.6.14",
    "vue-template-compiler": "^2.6.14",
    "vuetify": "^2.6.1",
    "webpack": "^4.46.0"
  },
  "devDependencies": {
    "@babel/eslint-parser": "^7.16.3",
    "@nuxtjs/eslint-config": "^8.0.0",
    "@nuxtjs/eslint-module": "^3.0.2",
    "@nuxtjs/vuetify": "^1.12.3",
    "eslint": "^8.4.1",
    "eslint-plugin-nuxt": "^3.1.0",
    "eslint-plugin-vue": "^8.2.0"
  }
}
# nuxt.config.js
export default {
  ssr: false,
  target: 'static',
  head: ...,
  css: [
    '~assets/fonts.css',
  ],
  plugins: [
  ],
  components: true,
  buildModules: [
    '@nuxtjs/eslint-module',
    '@nuxtjs/vuetify'
  ],
  modules: [
    '@nuxtjs/axios',
    '@nuxtjs/auth-next',
    '@nuxtjs/pwa'
  ],
  axios: {
    baseURL: '/api/',
    credentials: false
  },
  pwa: {
    manifest: {
      lang: 'en'
    }
  },
  router: {
    middleware: ['auth']
  },
  auth: ...,
  vuetify: ...,
  build: {
  }
}
# Dockerfile
FROM node:lts-alpine as builder

WORKDIR /app

COPY . .

RUN yarn install \
  --prefer-offline \
  --frozen-lockfile \
  --non-interactive \
  --production=false

RUN yarn add nuxt

RUN yarn build      # <----- Here, cause error
RUN yarn generate

RUN rm -rf node_modules && \
  NODE_ENV=production yarn install \
  --prefer-offline \
  --pure-lockfile \
  --non-interactive \
  --production=true

FROM node:lts-alpine

WORKDIR /app

#COPY --from=builder /app  .

ADD package.json ./
ADD nuxt.config.js ./
COPY --from=builder ./app/node_modules ./node_modules/
COPY --from=builder ./app/.nuxt ./.nuxt/
COPY --from=builder ./app/static ./static/
COPY --from=builder ./app/dist ./dist/

ENV NUXT_HOST=0.0.0.0
ENV NUXT_PORT=3001
ENV NODE_ENV=production
# docker-compose.yml
version: "3.8"

services:

  front:
    build: ./
    command: yarn start

    expose:
      - 3001

and I'm using docker compose build command to build.

CodePudding user response:

You need to have:

  • yarn add [email protected]
  • yarn generate only (no need for yarn build)
  • use Node v16 preferably (LTS being v18 but it's still okay IMO)
  • Related