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

Webpack: isomorphic-unfetch breaks node build due to wrong esm interop. #122

Closed
ealmansi opened this issue Aug 24, 2019 · 5 comments
Closed
Assignees
Labels

Comments

@ealmansi
Copy link

Bug Report

Requiring isomorphic-unfetch in a project built with Webpack targeting node leads to broken code. The issue is due to wrong interoperation between cjs & esm by isomorphic-unfetch.

How to Reproduce

$ mkdir bug-repro && cd bug-repro
$ yarn init -y
$ yarn add isomorphic-unfetch webpack webpack-cli
$ echo "require('isomorphic-unfetch')('https://goo.gl').then(console.log)" > index.js
$ npx webpack index.js --target node --mode development
$ node dist/main.js

Expected Result

> Response {
  ...
}

Actual Result

webpack:///./node_modules/isomorphic-unfetch/index.js?:3
		return __webpack_require__(/*! node-fetch */ "./node_modules/node-fetch/lib/index.mjs")(url.replace(/^\/\//g,'https://'), opts);
		                                                                                       ^

TypeError: __webpack_require__(...) is not a function

Context

In file packages/isomorphic-unfetch/index.js, you can find the following code: require('node-fetch'). However, the package node-fetch exposes the function fetch as the default export in an ESM module [1]. The correct way of requiring a default export from CommonJS is require('node-fetch').default; see [2].

[1] https://github.com/bitinn/node-fetch/blob/master/src/index.js#L34
[2] node-fetch/node-fetch#450 (comment)

Environment

→ lsb_release -a
Description:	Ubuntu 18.04.2 LTS
→ node --version
v10.15.3
→ yarn --version
1.15.2
→ npm ls isomorphic-unfetch webpack
[email protected]
├── [email protected] 
└── [email protected]
@ivan-kleshnin
Copy link

I'm getting

You must provide the URL of lib/mappings.wasm by calling SourceMapConsumer.initialize({ 'lib/mappings.wasm': ... }) before using SourceMapConsumer
    at readWasm

With nodemon + unfetch + slonik. Probably related to this issue.

karlhorky added a commit to upleveled/next-js-with-slonik-sept-2019 that referenced this issue Oct 19, 2019
`isomorphic-unfetch` causes an error in the production server bundle
because of an improper import on `node-fetch`, replacing it with
`cross-fetch` fixes it.

Ref: developit/unfetch#122
@karlhorky
Copy link

I had a similar issue to @ivan-kleshnin when using Slonik with Next.js in my project:

https://github.com/karlhorky/next.js-with-slonik

Fixed it in this commit by switching isomorphic-unfetch to cross-fetch, like @gabiseabra in emcasa/react-starter#12:

upleveled/next-js-with-slonik-sept-2019@a61f479

@karlhorky
Copy link

Awesome, thanks @danieltodonnell!

@danieltodonnell
Copy link

danieltodonnell commented Oct 20, 2019

I made a mistake and commented in the wrong issue :(

I meant to write "we will investigate" in the issue in faunadb that was affected by this issue.

@developit
Copy link
Owner

Released in 4.2.0. Sorry for the super long wait!

magicpalmtree added a commit to magicpalmtree/next-with-slonik that referenced this issue Oct 11, 2022
`isomorphic-unfetch` causes an error in the production server bundle
because of an improper import on `node-fetch`, replacing it with
`cross-fetch` fixes it.

Ref: developit/unfetch#122
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

5 participants