npm - 如果使用 docker 运行,则找不到模块

Posted

技术标签:

【中文标题】npm - 如果使用 docker 运行,则找不到模块【英文标题】:npm - module not found if run with docker 【发布时间】:2021-12-06 04:44:27 【问题描述】:

我正在尝试使用 docker 运行我的 vue 应用程序。在本地更新 npm 包后,出现以下错误:

-  Building for production...
 ERROR  Failed to compile with 1 error8:17:57 AM

This relative module was not found:

* ../internals/a-function in ./node_modules/core-js/internals/new-promise-capability.js
 ERROR  Build failed with errors.

在 Windows 下,一切正常。我已经尝试添加最新的 core-js 模块并将 package.json 恢复到稳定状态,但没有成功。我不知道我错过了什么。

我的 Dockerfile:

# develop stage
FROM node:lts-alpine as develop-stage
WORKDIR /app
COPY app/package*.json ./
RUN npm install
COPY app/. .

# build stage
FROM develop-stage as build-stage
RUN npm run build

# production stage
FROM nginx:alpine as production-stage
COPY --from=build-stage /app/dist /usr/share/nginx/html
EXPOSE 90
CMD ["nginx", "-g", "daemon off;"]

我的 package.json:


  "name": "frontend",
  "version": "0.1.0",
  "private": true,
  "scripts": 
    "serve": "vue-cli-service serve",
    "build": "vue-cli-service build",
    "lint": "vue-cli-service lint"
  ,
  "dependencies": 
    "@fortawesome/fontawesome-svg-core": "^1.2.36",
    "@fortawesome/free-solid-svg-icons": "^5.15.4",
    "@fortawesome/vue-fontawesome": "^2.0.6",
    "@growthbunker/vuedarkmode": "^0.5.56",
    "axios": "^0.21.4",
    "colormap": "^2.3.2",
    "core-js": "^3.18.2",
    "json-loader": "^0.5.7",
    "numeral": "^2.0.6",
    "prismjs": "^1.25.0",
    "pvw-visualizer": "^3.2.2",
    "splitpanes": "^2.3.8",
    "vue": "^2.6.11",
    "vue-json-editor": "^1.4.3",
    "vue-json-pretty": "^1.8.1",
    "vue-numeral-filter": "^2.0.0",
    "vue-numerals": "^4.0.6",
    "vue-plotly": "^1.1.0",
    "vue-prism-editor": "^1.3.0",
    "vue-remote-component": "^1.4.0",
    "vue-vtk-js": "^1.1.6",
    "vuescroll": "^4.17.3",
    "vuetify": "^2.5.10",
    "yaml-loader": "^0.6.0"
  ,
  "devDependencies": 
    "@vue/cli-plugin-babel": "^4.5.14",
    "@vue/cli-plugin-eslint": "^4.5.14",
    "@vue/cli-service": "^4.5.14",
    "babel-eslint": "^10.1.0",
    "eslint": "^6.8.0",
    "eslint-plugin-html": "^6.2.0",
    "eslint-plugin-vue": "^7.19.1",
    "sass": "~1.32.0",
    "sass-loader": "^10.0.0",
    "vue-cli-plugin-vuetify": "^2.4.3",
    "vue-template-compiler": "^2.6.11",
    "vuetify-loader": "^1.7.3"
  ,
  "eslintConfig": 
    "root": true,
    "env": 
      "node": true
    ,
    "extends": [
      "plugin:vue/essential",
      "eslint:recommended"
    ],
    "parserOptions": 
      "parser": "babel-eslint"
    ,
    "rules": 
  ,
  "browserslist": [
    "> 1%",
    "last 2 versions",
    "not dead"
  ]

【问题讨论】:

要查看详细错误,从 Dockerfile 中删除“npm install”,创建镜像和容器,连接容器的 bash 并输入“npm install”。它为您提供更具体的错误日志。 好主意@y.selimdogan,我会试试的! 【参考方案1】:

删除构建阶段,以某种方式修复它:

# develop stage
FROM node:lts-alpine as develop-stage
WORKDIR /app
COPY app/package*.json ./
RUN npm install
COPY app/. .

# # build stage
# FROM develop-stage as build-stage
RUN npm run build

# production stage
FROM nginx:alpine as production-stage
COPY --from=develop-stage /app/dist /usr/share/nginx/html
EXPOSE 90
CMD ["nginx", "-g", "daemon off;"]

【讨论】:

以上是关于npm - 如果使用 docker 运行,则找不到模块的主要内容,如果未能解决你的问题,请参考以下文章

如果实现 GooglePlayServicesClient.ConnectionCallbacks/OnConnectionFailedListener,则找不到主要活动类

从节点映像运行 docker 容器时找不到 Npm

npm:后安装未在 docker 中运行

Docker /bin/bash: nodemon: 找不到命令

vuejs 应用程序不使用 dockerized npm 运行

如何在 docker 容器中运行 npm 命令?