将 Vite / React App 部署到 GitHub Pages

2025-06-07

将 Vite / React App 部署到 GitHub Pages

只需按照以下简单步骤操作即可:

1.安装gh-pages软件包(ctrl+~在 VS Code 中打开终端)

npm install gh-pages --save-dev
Enter fullscreen mode Exit fullscreen mode

2. 在package.json文件中,在"build": "vite build",

"predeploy": "npm run build",
"deploy": "gh-pages -d dist",
Enter fullscreen mode Exit fullscreen mode

3. 在vite.config.js文件中,添加此行plugins: [react()],

base: "/YOUR_REPOSITORY_NAME",
Enter fullscreen mode Exit fullscreen mode

更改YOUR_REPOSITORY_NAME为您的 GitHub 存储库的名称。

4. 在终端输入

npm run deploy
Enter fullscreen mode Exit fullscreen mode

🎉 您的存储库中现在有一个gh-pages分支,并且您的应用程序已部署(您可以在 下检查Settings -> Pages

PS 要更新您的应用程序部署,只需npm run deploy再次运行该命令。

文章来源:https://dev.to/rashidshamloo/deploying-vite-react-app-to-github-pages-35hf
PREV
从头开始实现信号
NEXT
C# 中的取消标记