Vite3 Svelte3构建Web应用报错process is not defined
作者:天問
这篇文章主要介绍了Vite3 Svelte3构建Web应用报错:'process is not defined'解决方法详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
正文
Vite爬坑日记: 在使用 Vite3 + Svelte3 构建 Web 应用时,控制台报错:process is not defined。

Vite
问题原因
在 Vite 中 process.env 全局环境变量被移除了,相当于 process.env = null,如果项目中有依赖 process.env 这个对象,并进行了 get/set 操作时就会出现报错。
解决办法
- 在
vite.config.js配置文件中增加define: { "process.env": {} }
import { defineConfig, loadEnv } from "vite";
import { svelte } from "@sveltejs/vite-plugin-svelte";
import sveltePreprocess from "svelte-preprocess";
// https://vitejs.dev/config/
export default defineConfig({
plugins: [
svelte({
preprocess: sveltePreprocess(),
}),
],
define: {
"process.env": {}
}
})PS: 这里涉及到 Vite3 配置 / 共享选项 中的 define 变量。官网地址
define类型: Record<string, string>
定义全局常量替换方式。其中每项在开发环境下会被定义在全局,而在构建时被静态替换。
以上就是Vite3 Svelte3构建Web应用报错process is not defined的详细内容,更多关于Vite3 Svelte3构建Web报错的资料请关注脚本之家其它相关文章!
