admin管理员组文章数量:1314442
I installed Vite in my React project and set up Docker, but when building the container, I get the error: sh: 1: vite: not found. I don’t understand why this is happening, since Vite is installed in the project.
Before adding Vite, my project worked without errors. The package.json file looked like this:
"scripts": {
"start": "WATCHPACK_POLLING=true react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject"
}
Steps I took:
1. I installed Vite in the project using the command:
npm install vite
2. I updated the scripts in package.json:
"scripts": {
"dev": "vite",
"build": "vite build",
"serve": "vite preview"
},
"devDependencies": {
"react-refresh": "^0.16.0",
"vite": "^6.0.11"
}
3. I created the vite.config.js configuration file:
import { defineConfig } from 'vite';
import react from '@vitejs/plugin-react';
export default defineConfig({
plugins: [react()]
});
4. I updated the dev.Dockerfile, and now it looks like this:
FROM node:latest
RUN mkdir /app
WORKDIR /app
COPY package*.json ./
RUN npm install
COPY . .
CMD ["npm", "run", "dev"]
5. In docker-compose-dev.yml, I configured the frontend section:
frontend:
build:
context: ./frontend
dockerfile: dev.Dockerfile
volumes:
- .env:/app/.env
- ./frontend:/app
- /app/node_modules
env_file:
- .env
depends_on:
backend:
condition: service_started
6. When building and starting the container using the command
docker compose -f docker-compose-dev.yml up --build
I get the following error:
sh: 1: vite: not found
Please let me know if you need more code or additional details to help diagnose the issue.
I installed Vite in my React project and set up Docker, but when building the container, I get the error: sh: 1: vite: not found. I don’t understand why this is happening, since Vite is installed in the project.
Before adding Vite, my project worked without errors. The package.json file looked like this:
"scripts": {
"start": "WATCHPACK_POLLING=true react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject"
}
Steps I took:
1. I installed Vite in the project using the command:
npm install vite
2. I updated the scripts in package.json:
"scripts": {
"dev": "vite",
"build": "vite build",
"serve": "vite preview"
},
"devDependencies": {
"react-refresh": "^0.16.0",
"vite": "^6.0.11"
}
3. I created the vite.config.js configuration file:
import { defineConfig } from 'vite';
import react from '@vitejs/plugin-react';
export default defineConfig({
plugins: [react()]
});
4. I updated the dev.Dockerfile, and now it looks like this:
FROM node:latest
RUN mkdir /app
WORKDIR /app
COPY package*.json ./
RUN npm install
COPY . .
CMD ["npm", "run", "dev"]
5. In docker-compose-dev.yml, I configured the frontend section:
frontend:
build:
context: ./frontend
dockerfile: dev.Dockerfile
volumes:
- .env:/app/.env
- ./frontend:/app
- /app/node_modules
env_file:
- .env
depends_on:
backend:
condition: service_started
6. When building and starting the container using the command
docker compose -f docker-compose-dev.yml up --build
I get the following error:
sh: 1: vite: not found
Please let me know if you need more code or additional details to help diagnose the issue.
Share Improve this question asked Jan 30 at 11:30 SynchroSynchro 1,2694 gold badges25 silver badges61 bronze badges 3 |1 Answer
Reset to default 0Your vite
package is listed under devDependencies
. Maybe you have set your environment to production
(via NODE_ENV
), that's why npm install
does not install dev packages.
To fix it, you can either:
- Move
vite
todependencies
part, like this:
"scripts": {
"dev": "vite",
"build": "vite build",
"serve": "vite preview"
},
"dependencies": {
"vite": "^6.0.11"
},
"devDependencies": {
"react-refresh": "^0.16.0"
}
- or explicitly tell
npm
to install all packages with the flag--production=false
.
FROM node:latest
RUN mkdir /app
WORKDIR /app
COPY package*.json ./
RUN npm install --production=false
COPY . .
CMD ["npm", "run", "dev"]
By setting --production=false
, npm
will install all packages in your package.json
file, no matter what the value of NODE_ENV
is.
Finally, make sure that node_modules
is not copied in the build process by adding it to the .dockerignore
file.
本文标签: reactjsError vite not found after adding Vite to a React project in DockerStack Overflow
版权声明:本文标题:reactjs - Error vite: not found after adding Vite to a React project in Docker - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741969491a2407748.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
.env
file look like? – Triet Doan Commented Jan 30 at 11:37volumes:
block entirely, including both the line that replaces all of/app
with host content and the following line that replaces/app/node_modules
with an anonymous volume that does not get updated. – David Maze Commented Jan 30 at 14:18