为什么 docker-compose 在构建过程中未找到 vite?

Fre*_*sch 2 npm docker reactjs docker-compose vite

我有一个用 Vite 构建的 React 前端应用程序。当我运行 Docker 时,出现以下错误

\n
[+] Running 1/1\n \xe2\xa0\xbf Container client-client-1  Recreated                                                                0.2s\nAttaching to client-client-1\nclient-client-1  |\nclient-client-1  | > client@0.0.0 dev\nclient-client-1  | > vite\nclient-client-1  |\nclient-client-1  | sh: 1: vite: not found\nclient-client-1 exited with code 127\n
Run Code Online (Sandbox Code Playgroud)\n

下面你可以看到我的 Dockerfile:

\n
FROM node\n\nWORKDIR /usr/src/app\n\nCOPY ./package.json .\n\nRUN npm i\n\nCOPY . .\n\n\nCMD ["npm", "run", "dev"]\n
Run Code Online (Sandbox Code Playgroud)\n

我尝试在构建 docker 容器期间添加一个命令来安装 vite,但它不起作用。\n您检查下面的 docker-compose 文件

\n
[+] Running 1/1\n \xe2\xa0\xbf Container client-client-1  Recreated                                                                0.2s\nAttaching to client-client-1\nclient-client-1  |\nclient-client-1  | > client@0.0.0 dev\nclient-client-1  | > vite\nclient-client-1  |\nclient-client-1  | sh: 1: vite: not found\nclient-client-1 exited with code 127\n
Run Code Online (Sandbox Code Playgroud)\n

这是我的 package.json

\n
{\n  "name": "client",\n  "private": true,\n  "version": "0.0.0",\n  "type": "module",\n  "scripts": {\n    "dev": "vite",\n    "build": "tsc && vite build",\n    "preview": "vite preview",\n    "lint": "eslint .",\n    "lint:fix": "eslint --fix ."\n  },\n  "dependencies": {\n    "eslint": "^8.36.0",\n    "eslint-config-airbnb": "^19.0.4",\n    "react": "^18.2.0",\n    "react-dom": "^18.2.0"\n  },\n  "devDependencies": {\n    "@testing-library/jest-dom": "^5.16.5",\n    "@testing-library/react": "^14.0.0",\n    "@types/react": "^18.0.28",\n    "@types/react-dom": "^18.0.11",\n    "@typescript-eslint/eslint-plugin": "^5.54.1",\n    "@typescript-eslint/parser": "^5.54.1",\n    "@vitejs/plugin-react": "^3.1.0",\n    "eslint": "^8.35.0",\n    "eslint-config-airbnb": "^19.0.4",\n    "eslint-config-airbnb-typescript": "^17.0.0",\n    "eslint-config-prettier": "^8.7.0",\n    "eslint-plugin-import": "^2.27.5",\n    "eslint-plugin-jsx-a11y": "^6.7.1",\n    "eslint-plugin-prettier": "^4.2.1",\n    "eslint-plugin-react": "^7.32.2",\n    "eslint-plugin-react-hooks": "^4.6.0",\n    "jsdom": "^21.1.0",\n    "prettier": "^2.8.4",\n    "typescript": "^4.9.3",\n    "vite": "^4.2.0"\n  }\n}\n
Run Code Online (Sandbox Code Playgroud)\n

小智 11

更改 docker-compose 以将匿名持久卷安装到 node_modules 以防止本地覆盖它

这对我有用

version: '3.9'

services:
  client:
    build: .
    ports:
      - 5173:5173
    volumes:
      - .:/usr/src/app
      - /usr/src/app/node_modules
Run Code Online (Sandbox Code Playgroud)