大约六个月前,我用 docker 部署了一个 nuxt 项目,它运行良好,但现在,我需要重建它,但我得到了这个错误:
> [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.
我根本没有改变我的代码,它之前就可以工作。我猜是关于节点图像版本,因为它是
node:lts-alpine
,所以我尝试了node:19-alpine3.15
,node:19-alpine
,node:19
,node:18-alpine3.15
,node:18-alpine
,node:18
,node:16-alpine
,node:14-alpine
,但他们都没有解决我的问题。
该错误会在
axios
模块中抛出,在 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)
}
我真的不知道我的项目出了什么问题,但我会附上一些我认为可能有用的文件。
# 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
我正在使用
docker compose build
命令来构建。
您需要:
yarn add [email protected]
yarn generate
(无需yarn build
)