vite 打包不生成dist文件夹
时间: 2023-06-15 13:02:57 浏览: 105
如果你使用 Vite 进行打包却没有生成 `dist` 文件夹的话,可能有以下几个原因:
1. 没有正确配置 `build` 命令
在 `package.json` 文件中,`build` 命令应该被配置为 `vite build`,如下所示:
```json
{
"scripts": {
"build": "vite build"
}
}
```
2. build 执行时没有权限
如果你在执行 `npm run build` 命令时,遇到了类似下面这样的错误:
```
sh: 1: vite: not found
npm ERR! code ELIFECYCLE
npm ERR! syscall spawn
npm ERR! file sh
npm ERR! errno ENOENT
npm ERR! my-app@0.1.0 build: `vite build`
npm ERR! spawn ENOENT
npm ERR!
npm ERR! Failed at the my-app@0.1.0 build script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
```
那么可能是因为你没有给 `build` 命令执行的权限。你需要在终端中执行以下命令,给 `node_modules/.bin` 目录下的文件加上可执行权限:
```bash
chmod -R +x node_modules/.bin
```
3. 缺少 `build` 插件
在 Vite 中,`build` 命令需要使用 `@vitejs/plugin-vue` 或 `@vitejs/plugin-react` 等插件。如果你没有安装这些插件,则在执行 `npm run build` 命令时,会提示以下错误:
```
error [vite] Plugin not found: /path/to/project/node_modules/@vitejs/plugin-vue/index.js
```
你需要在 `package.json` 文件中添加以下依赖:
```json
{
"dependencies": {
"@vitejs/plugin-vue": "^1.8.4"
}
}
```
然后在 `vite.config.js` 文件中引入这个插件:
```javascript
import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
export default defineConfig({
plugins: [
vue()
]
})
```
以上是我能想到的一些可能的原因,希望能帮到你。