我如何汇报“过程” Vite Dev服务器中的节点模块?
在我的Vite项目中,我取决于一个模块,该模块在其功能之一中使用Process
节点global。我不会从我的代码中调用此功能,但是当我导入模块时, vite dev 服务器仍然会给我这个错误:
Uncaught ReferenceError: process is not defined
有趣的是,当我创建生产构建时,我看不到此错误。
如何使用no-op polyfill Process
,以使Vite Dev Server停止失败?
In my Vite project, I am depending on a module that makes use of the process
Node global in one of its functions. I don't call this function from my code, but the Vite dev server still gives me this error when I import the module:
Uncaught ReferenceError: process is not defined
Interestingly, I don't see this error when I create a production build.
How can I polyfill process
with a no-op so that the Vite dev server stops failing?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
今天,我在使用crolup + vite的一个React项目中也遇到了同样的问题,这就是我解决的方法,使用 Fabiano Taioli的中型作品。
vite需要node.js polyfills
您需要添加一些polyfill插件以允许节点Globals,例如
Process
。幸运的是,您可以简单地编辑(或创建)vite.config.js
。示例
vite.config.js
是一个示例,其中包括使用
nodeglobalspolyfillplugin
to polyfillprocess
。它还包括许多其他节点全球群体,因此在闲暇时删除。将上述示例工作所需的开发依赖项
需要添加一些依赖项。尤其:
I had the same issue today in a React project using rollup + vite and here's how I solved it, using this Medium piece by Fabiano Taioli.
Vite needs Node.js polyfills
You need to add some polyfill plugins to allow Node globals, such as
process
. Fortunately, you can simply edit (or create) thevite.config.js
.Example
vite.config.js
Below is an example which includes using
NodeGlobalsPolyfillPlugin
to polyfillprocess
. It also includes many other node globals so remove at your leisure.Development dependencies required
To make the above example work as is you'll need to add some dependencies. In particular:
我正在使用 node-stdlib-browser ,并且非常适合它我。
以下是我的最终
vite.config.ts
I'm using node-stdlib-browser, and it works really well for me.
And the following is my final
vite.config.ts
在2024年,我建议使用 vite-plugin-node-polyfills 包裹。
PROS:更容易使用,配置和理解
cons:具有很多DEV依赖性
步骤:
1。)添加“ Vite-Plugin-Node-Polyfills”为DEV依赖关系:
并将“ nodepolyfills”添加为来自“ vite-plugin-node-polyfills”的插件:
2。)打开您的vite config file (例如“ vite.config.js”) 这一切都需要吗?
Vite不包括节点的核心模块。使用此软件包,您可以将任何node.js内置模块,多填充特定的全球群体或覆盖特定模块的默认polyfills覆盖。 npmjs上的检查包
In 2024 I would advise usage of vite-plugin-node-polyfills package.
Pros: It's much easier to use, configure and understand
Cons: Has lot of dev dependencies
Steps:
1.) Add "vite-plugin-node-polyfills" as dev dependency:
2.) Open your Vite config file (e.g. "vite.config.js") and add "nodePolyfills" as a plugin from "vite-plugin-node-polyfills" with this config:
Why is this all needed?
Vite does not include Node's Core Modules. With this package you can polyfill any Node.js Built-in Module, polyfill specific globals or override the default polyfills for specific modules. Check package on npmjs
替代方案是添加到您的index.html
An alternative is adding to your index.html
请使用“ Esbuild-Plugin-Polyfill节点”插件。
https://npm.io/package/esbuild-plugin-plugin-polugin-polyfill-polyfill-node
这为我解决了问题,无需在脚本标签中手动添加和分配
global = window
。Please use 'esbuild-plugin-polyfill-node' plugin.
https://npm.io/package/esbuild-plugin-polyfill-node
This solved the problem for me, and NO NEEDED of manually adding and assigning
global = window
in script tag.