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

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

ViteJS with docker cannot find module npm run install

问题

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

Docker文件:

FROM node

WORKDIR /app

COPY package*.json .

RUN npm install

COPY . .

CMD npm run dev

EXPOSE 4000

ENV NODE_ENV development

Docker Compose:

version: '3.8'

services:
  app:
    build: .
    container_name: train_vite
    working_dir: /app/
    ports:
      - "4000:4000"
    volumes:
      - ./:/var/www/html/app
    command:
      - npm run install
      - npm run dev
    environment:
      NODE_ENV: development

vite.config.ts:

import { defineConfig } from 'vite'
import react from '@vitejs/plugin-react-swc'

// https://vitejs.dev/config/
export default defineConfig({
  plugins: [react()],
  server: {
    watch: {
      usePolling: true,
    },
    host: true,
    strictPort: true,
    port: 4000
  }
})

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

英文:

with my Docker file

FROM node

WORKDIR /app

COPY package*.json .

RUN npm install

COPY . .

CMD npm run dev

EXPOSE 4000

ENV NODE_ENV development

and docker compose

version: '3.8'

services:
  app:
    build: .
    container_name: train_vite
    working_dir: /app/
    ports:
      - "4000:4000"
    volumes:
      - ./:/var/www/html/app
    command:
      - npm run install
      - npm run dev
    environment:
      NODE_ENV: development

and vite.config.ts

import { defineConfig } from 'vite'
import react from '@vitejs/plugin-react-swc'

// https://vitejs.dev/config/
export default defineConfig({
  plugins: [react()],
  server:{watch:{
    usePolling:true,
    },
    host:true,
    strictPort:true,
    port:4000
  }
})

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 可以在组合定义中指定为字符串:

"/run/this/command witharg"

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

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

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

将示例更新为字符串:

version: '3.8'

services:
  app:
    build: .
    container_name: train_vite
    working_dir: /app/
    ports:
      - "4000:4000"
    volumes:
      - ./:/var/www/html/app
    command: "npm run dev"

或者作为数组

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

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

英文:

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

"/run/this/command witharg"

or an array of the command and its arguments.

[ "/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:

version: '3.8'

services:
  app:
    build: .
    container_name: train_vite
    working_dir: /app/
    ports:
      - "4000:4000"
    volumes:
      - ./:/var/www/html/app
    command: "npm run dev"

or as an array

    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:

确定