question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Error when using isomorphic-git with vitejs

See original GitHub issue

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 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:

  1. Setup a new project using npm init @vitejs/app
  2. Add isomorphic-git as a dependency
  3. 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:closed
  • Created 2 years ago
  • Comments:9 (4 by maintainers)

github_iconTop GitHub Comments

2reactions
joelspadincommented, Jul 5, 2021

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:

npm i -D ./plugins/plugin-cdn

Then use it in your vite.config.ts to grab a browserified version of isomorphic-git:

import cdn from '@vitejs/plugin-cdn';
import { defineConfig } from 'vite';

// https://vitejs.dev/config/
export default defineConfig({
    plugins: [
        cdn('skypack', {
            'isomorphic-git': '^1.8.3',
        }),
    ],
});

I haven’t tested whether the package actually works yet, but at least I’m past the error when importing it.

1reaction
joelspadincommented, Jul 13, 2021

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 for vite 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:

import reactRefresh from '@vitejs/plugin-react-refresh';
import { defineConfig } from 'vite';

// https://vitejs.dev/config/
export default defineConfig({
    plugins: [reactRefresh()],
    resolve: {
        alias: {
            buffer: 'buffer',
            'node-fetch': 'isomorphic-fetch',
            http: 'stream-http',
            https: 'https-browserify',
            process: 'process/browser',
            stream: 'stream-browserify',
            // https://github.com/nodejs/readable-stream/issues/348
            'readable-stream': 'vite-compatible-readable-stream',
            string_decoder: 'string_decoder',
            url: 'url',
            util: 'util',
        },
    },
});

(npm install each of the packages in the alias map’s values.)

I have an additional polyfills.js that I’m importing in my main script file to handle Node globals:

import { Buffer } from 'buffer';
import * as process from 'process';

window.global = window;
window.process = process;
window.Buffer = Buffer;

I’m also using those polyfills to get @octokit/rest.js working, so some of that may not be necessary for just isomorphic-git.

Read more comments on GitHub >

github_iconTop 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 >

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