Error: Module "events" has been externalized for browser compatibility

See original GitHub issue

Describe the bug

After upgrading to Vite 3.0.2 (since 2.X version has some production level bugs for me) - the production version started working, but the development is crashing due to the following error:

Uncaught Error: Module "events" has been externalized for browser compatibility. Cannot access "events.EventEmitter" in client code. get events:9 js Parser.js:133 __require chunk-QOVRSCHT.js:11 js index.js:1 __require chunk-QOVRSCHT.js:11 <anonymous> HtmlParser.js:1 [events:9:12](browser-external:events) get events:9 js Parser.js:133 __require chunk-QOVRSCHT.js:11 js index.js:1 __require chunk-QOVRSCHT.js:11 <anonymous> HtmlParser.js:1 InnerModuleEvaluation self-hosted:2361 InnerModuleEvaluation self-hosted:2361 InnerModuleEvaluation self-hosted:2361 InnerModuleEvaluation self-hosted:2361 InnerModuleEvaluation self-hosted:2361 InnerModuleEvaluation self-hosted:2361 evaluation self-hosted:2322

After removing the mentioned dependency ‘HtmlParser’ -> https://www.npmjs.com/package/react-html-parser -> the installation seems to work.

Reproduction

https://stackblitz.com/edit/react-rj6cc2

System Info

System:
    OS: macOS 12.4
    CPU: (12) x64 Intel(R) Core(TM) i7-9750H CPU @ 2.60GHz
    Memory: 36.39 MB / 16.00 GB
    Shell: 5.8.1 - /bin/zsh
  Binaries:
    Node: 16.13.1 - /usr/local/bin/node
    Yarn: 1.22.19 - /usr/local/bin/yarn
    npm: 8.1.2 - /usr/local/bin/npm
    Watchman: 2022.07.04.00 - /usr/local/bin/watchman
  Browsers:
    Chrome: 103.0.5060.114
    Firefox Developer Edition: 103.0
    Safari: 15.5
  npmPackages:
    @vitejs/plugin-react: 2.0.0 => 2.0.0 
    vite: 3.0.0 => 3.0.0

Used Package Manager

yarn

Logs

No response

Validations

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:18 (5 by maintainers)

github_iconTop GitHub Comments

2reactions
justin0mcateercommented, Aug 7, 2022

See that link on Stack Overflow, you need to use a ‘resolve’ block and specifically map the ‘events’ import to a file on that polyfill. Also, there is a newer version of the polyfill: https://github.com/FredKSchott/rollup-plugin-polyfill-node

Look in the ‘polyfills’ directory and you will see all of the node modules that you can alias.

Get Outlook for Androidhttps://aka.ms/AAb9ysg


From: Lionel @.> Sent: Saturday, August 6, 2022 12:06:31 PM To: vitejs/vite @.> Cc: justin0mcateer @.>; Comment @.> Subject: Re: [vitejs/vite] Error: Module “events” has been externalized for browser compatibility (Issue #9238)

Fighting with the polypill nightmare. Anyone is able to fix the issue with Sveltekit/vite?

This is the config I’m using (process and buffer are not warning anymore so I guess it’s “working” but I’m stuck on events)

import nodePolyfills from “rollup-plugin-polyfill-node”;

import { defineConfig } from “vite”; import { svelte } from @.***/vite-plugin-svelte";

export default defineConfig({ plugins: [nodePolyfills({ include: null }), svelte()], });

— Reply to this email directly, view it on GitHubhttps://github.com/vitejs/vite/issues/9238#issuecomment-1207248242, or unsubscribehttps://github.com/notifications/unsubscribe-auth/AAT4Q7I67SHOSWK57EZTYY3VX2LRPANCNFSM54CD2BLA. You are receiving this because you commented.Message ID: @.***>

2reactions
justin0mcateercommented, Aug 4, 2022

The StackOverflow answer mentioned above also worked for me…

Read more comments on GitHub >

github_iconTop Results From Across the Web

Uncaught Error: Module "events" has been externalized for ...
Error : Module "events" has been externalized for browser compatibility. Cannot access "events.EventEmitter" in client code. git repo link : ...
Read more >
Module "path" has been externalized for browser compatibility
I am using Vite for some front end development, and when I used an external library which is using the "path" library, it...
Read more >
module events has been externalized for browser compatibility.
I think is purely error of vite server to try load node_modules in webbrowser of client side. If i found a solution for...
Read more >
vite-plugin-node-polyfills - NPM Package Overview - Socket.dev
Module "stream" has been externalized for browser compatibility. Cannot access "stream.Readable" in client code.
Read more >
Error when using Airtable (and other npm packages) with ...
Uncaught Error : Module "stream" has been externalized for browser compatibility and cannot be accessed in client code. at Object.get ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found