ViteJS 配合 Docker 无法找到模块 npm run install.

huangapple go评论103阅读模式
英文:

ViteJS with docker cannot find module npm run install

问题

以下是您的Docker文件、Docker Compose和vite.config.ts的翻译部分:

Docker文件:

  1. FROM node
  2. WORKDIR /app
  3. COPY package*.json .
  4. RUN npm install
  5. COPY . .
  6. CMD npm run dev
  7. EXPOSE 4000
  8. ENV NODE_ENV development

Docker Compose:

  1. version: '3.8'
  2. services:
  3. app:
  4. build: .
  5. container_name: train_vite
  6. working_dir: /app/
  7. ports:
  8. - "4000:4000"
  9. volumes:
  10. - ./:/var/www/html/app
  11. command:
  12. - npm run install
  13. - npm run dev
  14. environment:
  15. NODE_ENV: development

vite.config.ts:

  1. import { defineConfig } from 'vite'
  2. import react from '@vitejs/plugin-react-swc'
  3. // https://vitejs.dev/config/
  4. export default defineConfig({
  5. plugins: [react()],
  6. server: {
  7. watch: {
  8. usePolling: true,
  9. },
  10. host: true,
  11. strictPort: true,
  12. port: 4000
  13. }
  14. })

如果您有其他问题或需要进一步的帮助,请随时告诉我。

英文:

with my Docker file

  1. FROM node
  2. WORKDIR /app
  3. COPY package*.json .
  4. RUN npm install
  5. COPY . .
  6. CMD npm run dev
  7. EXPOSE 4000
  8. ENV NODE_ENV development

and docker compose

  1. version: '3.8'
  2. services:
  3. app:
  4. build: .
  5. container_name: train_vite
  6. working_dir: /app/
  7. ports:
  8. - "4000:4000"
  9. volumes:
  10. - ./:/var/www/html/app
  11. command:
  12. - npm run install
  13. - npm run dev
  14. environment:
  15. NODE_ENV: development

and vite.config.ts

  1. import { defineConfig } from 'vite'
  2. import react from '@vitejs/plugin-react-swc'
  3. // https://vitejs.dev/config/
  4. export default defineConfig({
  5. plugins: [react()],
  6. server:{watch:{
  7. usePolling:true,
  8. },
  9. host:true,
  10. strictPort:true,
  11. port:4000
  12. }
  13. })

throws an error like this
ViteJS 配合 Docker 无法找到模块 npm run install.
the command I run is
docker compose up

I try to run a docker image with the app folder linked to the docker folder (volume) to see the changes on live.

答案1

得分: 1

一个单独的 command 可以在组合定义中指定为字符串:

  1. "/run/this/command witharg"

或者是命令及其参数的数组。

  1. [ "/run/this/command", "witharg" ]

现有的定义尝试执行一个名为 npm run install 的二进制文件,并带有参数 npm run dev

将示例更新为字符串:

  1. version: '3.8'
  2. services:
  3. app:
  4. build: .
  5. container_name: train_vite
  6. working_dir: /app/
  7. ports:
  8. - "4000:4000"
  9. volumes:
  10. - ./:/var/www/html/app
  11. command: "npm run dev"

或者作为数组

  1. command: [ "/usr/local/bin/npm", "run", "dev" ]

要在启动时运行多个命令,构建需要在容器中有一个脚本,然后将该脚本用作 commandentrypoint

英文:

A single command can be specified in a compose definition as a string:

  1. "/run/this/command witharg"

or an array of the command and its arguments.

  1. [ "/run/this/command", "witharg" ]

The existing definition is attempting to execute a binary called
npm run install with the argument npm run dev

Updating the example as a string:

  1. version: '3.8'
  2. services:
  3. app:
  4. build: .
  5. container_name: train_vite
  6. working_dir: /app/
  7. ports:
  8. - "4000:4000"
  9. volumes:
  10. - ./:/var/www/html/app
  11. command: "npm run dev"

or as an array

  1. command: [ "/usr/local/bin/npm", "run", "dev" ]

To run multiple commands at startup, the build would need a script in the container and that script used as a command or entrypoint.

huangapple
  • 本文由 发表于 2023年2月8日 14:24:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/75382041.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定