Error when using isomorphic-git with vitejs
See original GitHub issueI setup a react typescript project using npm init @vitejs/app
and added isomorphic-git as a dependency.
On running the project locally, I am getting the following error:
Uncaught TypeError: Cannot read property 'from' of undefined
at node_modules/safe-buffer/index.js (index.js:11)
at __require (chunk-KYLA5N2V.js?v=de250cfb:12)
at node_modules/sha.js/hash.js (hash.js:1)
at __require (chunk-KYLA5N2V.js?v=de250cfb:12)
at node_modules/sha.js/sha1.js (sha1.js:11)
at __require (chunk-KYLA5N2V.js?v=de250cfb:12)
at index.js:2
Browser: Brave Version 1.24.86 Chromium: 90.0.4430.212 (Official Build) (64-bit) Node version: 12.14.1
Steps to reproduce:
- Setup a new project using
npm init @vitejs/app
- Add isomorphic-git as a dependency
- Use any method of ismorphic-git in the project
This project can be cloned to reproduce the error
I have been using ismorphic-git with a project created using create-react-app
and it’s working fine. Only facing this issue with vitejs.
Issue Analytics
- State:
- Created 2 years ago
- Comments:9 (4 by maintainers)
Top Results From Across the Web
`vite` SSR module error when importing isomorphic library ...
I am using an isomorphic library supabase-js. When I import it in a file that is sourced by globalSetup , it throws following...
Read more >Developers - Error when using isomorphic-git with vitejs -
I setup a react typescript project using npm init @vitejs/app and added isomorphic-git as a dependency. On running the project locally, I am...
Read more >Error Codes - isomorphic-git
Exhausted tries. AddingRemoteWouldOverwrite. Adding remote { remote } would overwrite the existing remote. Use "force: true" to override.
Read more >module has been externalized for browser compatibility
Fetching a github repo in react gives a "Module "stream" has been externalized for browser compatibility and cannot be accessed in client code"...
Read more >reactjs - Octokit.js not working with Vite. Module externalized ...
As in, I expect to make requests using octokit perfectly fine. What am I experiencing? https://github.com/vitejs/vite/issues/5963. An issue akin ...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
I think this is caused because Vite doesn’t attempt to polyfill Node builtins. I couldn’t get a rollup polyfill working, but I did find an alternative solution at https://github.com/vitejs/vite/issues/2618#issuecomment-804399828. With the CDN plugin, you can make Vite use a browserified version:
First, since the CDN plugin is still a work in progress, copy this into your project https://github.com/vitejs/vite/tree/plugin-cdn/packages/plugin-cdn and install it as a dependency:
Then use it in your vite.config.ts to grab a browserified version of isomorphic-git:
I haven’t tested whether the package actually works yet, but at least I’m past the error when importing it.
The above works when running the vite dev server, but I ran into issues when running
vite build
. If I understand the issue correctly, a node builtins rollup polyfill would work but only forvite build
and not when running the dev server. I just aliased each node module to a polyfill instead:My vite.config.ts now looks like this:
(
npm install
each of the packages in thealias
map’s values.)I have an additional polyfills.js that I’m importing in my main script file to handle Node globals:
I’m also using those polyfills to get
@octokit/rest.js
working, so some of that may not be necessary for just isomorphic-git.