我目前在使用 GitHub Actions 将基于 Vite 的 React 应用程序部署到 AWS 时遇到问题。部署过程成功完成,但是当我尝试运行应用程序时,我在浏览器控制台中遇到以下错误:“未捕获的错误:服务 firestore 不可用”。
我正在使用这个vite.config.ts
import { defineConfig } from 'vite'
import react from '@vitejs/plugin-react'
import viteTsconfigPaths from 'vite-tsconfig-paths'
import commonjs from '@rollup/plugin-commonjs';
export default defineConfig({
// depending on your application, base can also be "/"
base: '',
resolve: {
alias: {
'aws-sdk': 'aws-sdk/dist/aws-sdk.min.js',
},
},
define: {
global: 'globalThis',
},
plugins: [react(), viteTsconfigPaths(), updateCommonjsPlugin()],
server: {
// this ensures that the browser opens upon server start
open: true,
// this sets a default port to 3000
port: 3000,
watch: {
followSymlinks: false,
},
},
build: {
minify: false,
sourcemap: false,
},
})
function updateCommonjsPlugin(): import('vite').PluginOption {
const commonJs22 = commonjs({
include: [/node_modules/],
extensions: ['.js', '.cjs'],
strictRequires: true,
})
return {
name: 'new-common-js',
apply: 'build',
config: (config) => {
return {
...config,
plugins: [...(config.plugins || []), commonJs22],
}
},
}
}
需要帮助并提前致谢。
Vite Node API 的 CJS 版本已弃用。您应该更新您的文件或框架以导入 Vite 的 ESM 版本。
在基础的Vite项目中,请确保:
vite.config.js 文件内容使用 ESM 语法。 最接近的 package.json 文件具有 "type": "module",或使用 .mjs/.mts 扩展名,例如vite.config.mjs 或 vite.config.mts.
如果 package.json 中不包含 "type": "module",Vite 会生成不同的文件扩展名以兼容 Node.js。 .js 将变为 .mjs,.cjs 将变为 .js。