Error: Module "http" has been externalized for browser compatibility... #4479
-
So I'm trying to use a library ("redoc") that uses some Node builtins. I have now spent many hours trying to understand how to polyfill/shim the dependencies in Vite, but I'm quite stuck. I get this error message in the browser:
I have looked into @rollup/plugin-node-resolve, nodepolyfills-plugin, etc etc, but nothing seems to solve the problem. Is there a general recipe for working around this issue? |
Beta Was this translation helpful? Give feedback.
Replies: 4 comments 18 replies
-
if you take a look at https://github.com/EvinqWang/vite_vue3_web3/blob/main/vite.config.ts and https://github.com/EvinqWang/vite_vue3_web3/blob/main/index.html you can see an example of how to polyfill vite with browserify modules |
Beta Was this translation helpful? Give feedback.
-
Thanks @Niputi for your help, but I think we need a detailed answer from the team and @yyx990803. I saw some discussions about the way node_modules are processed by Vite. I've seen a lot of issues related to this (for instance with the AWS Amplify SDK). I tried to use google-spreadsheet and even with your example and quite a lot of research with trials/errors this doesn't work. Vite is phenomenal, the speed and DX we have is amazing, but it can be a deal breaker if we can not use some libs (even if these libs are supposed to work primarily on the server side). |
Beta Was this translation helpful? Give feedback.
-
The following import { defineConfig } from "vite";
import react from "@vitejs/plugin-react";
import viteTsconfigPaths from "vite-tsconfig-paths";
import svgrPlugin from "vite-plugin-svgr";
// https://vitejs.dev/config/
export default defineConfig({
// https://github.com/vitejs/vite/issues/1973
define: {
// "process.env": process.env,
// // By default, Vite doesn't include shims for NodeJS/
// // necessary for segment analytics lib to work
"global": {},
},
resolve: {
alias: {
process: "process/browser",
buffer: "buffer",
crypto: "crypto-browserify",
stream: "stream-browserify",
assert: "assert",
http: "stream-http",
https: "https-browserify",
os: "os-browserify",
url: "url",
util: "util",
},
},
plugins: [react(), viteTsconfigPaths(), svgrPlugin()],
}); |
Beta Was this translation helpful? Give feedback.
-
I came here from importing
|
Beta Was this translation helpful? Give feedback.
if you take a look at https://github.com/EvinqWang/vite_vue3_web3/blob/main/vite.config.ts and https://github.com/EvinqWang/vite_vue3_web3/blob/main/index.html you can see an example of how to polyfill vite with browserify modules