Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clone fails on Node: global.fetch is not a function #938

Open
strogonoff opened this issue Nov 25, 2019 · 9 comments
Open

Clone fails on Node: global.fetch is not a function #938

strogonoff opened this issue Nov 25, 2019 · 9 comments

Comments

@strogonoff
Copy link

strogonoff commented Nov 25, 2019

From a quick scan, it looks like as of this change in January 2019 http module no longer checks for global.fetch, which excludes Node environment.

I was accidentally stuck on 0.50 version, and once I updated (for the sake of fastCheckout and other niceties) I’m getting the following stack trace when cloning:

  16:00:17.707 › UnhandledRejection TypeError: global.fetch is not a function
      at http (webpack-internal:///./node_modules/isomorphic-git/dist/for-future/isomorphic-git/index.js:4931:28)
      at Function.discover (webpack-internal:///./node_modules/isomorphic-git/dist/for-future/isomorphic-git/index.js:5162:21)
      at fetchPackfile (webpack-internal:///./node_modules/isomorphic-git/dist/for-future/isomorphic-git/index.js:5975:42)
      at fetch (webpack-internal:///./node_modules/isomorphic-git/dist/for-future/isomorphic-git/index.js:5840:22)
      at Module.clone (webpack-internal:///./node_modules/isomorphic-git/dist/for-future/isomorphic-git/index.js:6321:42)

From cursory search, it seems like global.fetch isn’t supposed to be available on Node. Is reinstating the previous Node-compatible state of http’s fetching logic on the table?

@strogonoff
Copy link
Author

strogonoff commented Nov 25, 2019

I’m not sure if I’m missing something, since no one else reports such an issue.

So far I made it work by jury-rigging node-fetch’s fetch() onto global:

import fetch from 'node-fetch';
(global as any).fetch = fetch;

@billiegoose
Copy link
Member

The node version doesn't use fetch at all... how are you importing isomorphic-git? Are you building a Node application with Webpack?

@strogonoff
Copy link
Author

@wmhilton I’m building an Electron one, and yes, this happens on Node process side. It does a Webpack pass during build🤔

I’m not aware if Webpack supplies global.fetch. Generally speaking you’re hinting it’s likely an issue on my side and you don’t think it’s a regression in isomorphic-git’s Node support?

@billiegoose
Copy link
Member

billiegoose commented Feb 4, 2020

Yes... it's really a webpack configuration issue. Webpack is probably trying to use the module file instead of the main file, but

  • the module file uses ES6 modules and is "for web" while
  • the main file uses CommonJS modules and is "for node"

Which 99% of the time is right, because Webpack is usually not used on Node.js apps. You can probably make Webpack use the "right" version with an alias to dist/for-node/isomorphic-git/index.js but if you're using webpack and isomorphic-git on both the node-side and the browser-side of an Electron app, you'd need separate webpack config files....

Anyway, as you can see it's obviously a bit of a mess. And I am going to fix this in version 1.0 by pulling this troublesome bit out of the library completely and make users provide an HTTP client, just like what I already do for fs. That way the code doesn't have to try and detect whether it's running in Node or be compiled differently.

@strogonoff
Copy link
Author

strogonoff commented Feb 5, 2020

Understood, very clear. Going to take note but keep global.fetch for now, fingers crossed no other incompatibilities crop up. Thank you for awesome work!

I guess Webpack is more for browser-based code, but this mainstream Electron builder library builds on top of Webpack to transpile/bundle both Node and in-browser code. Works impressively well out of the box but that Webpack config is not for the weak of heart (or subject to deadlines) to fiddle with (which I am so I don’t, unless something’s really broken).

@davidenke
Copy link

I have the same issue as well with a quite similar setup using rollup and deskgap.
Since I could configure @rollup/plugin-node-resolve to use only the main fields and ignore the module entries, it fails for other packages during bundling. Unfortunately it can't be configured per dependency, so I have to stick to the workaround of @strogonoff as well.

@wmhilton Maybe it would help to release a meta package for node usage (e.g. isomorphic-git-node which includes just the main field: thus it can be consumed by bundlers without further configuration...?

@billiegoose
Copy link
Member

Hi @davidenke ! See if the solution I hit upon in v1.0.0 works for you. Basically the http bit has been split off, so now you do:

import * as git from 'isomorphic-git'
import http from 'isomorphic-git/http/node'
// or
import http from 'isomorphic-git/http/web'

Full details and examples at https://isomorphic-git.org/docs/http

@billiegoose
Copy link
Member

@strogonoff ooh! what's the name of this mainstream Electron builder library that uses Webpack to transpile and bundle both? I was actually trying to do something similar the other day from scratch using Webpack (and failed miserably).

@strogonoff
Copy link
Author

@wmhilton I believe https://www.electron.build? That’s what we’re using!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants