You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
node-fetch 2.1.2 broken with webpack (node-fetch 1.7.3 works) #450
With node-fetch 2.1.2 you have to use the ES6 module import form:
import fetch from 'node-fetch'
If you use the commonjs form:
const fetch = require('node-fetch')
then the resulting bundle won't work properly:
node-fetch-webpack/bundle.js:103
fetch('https://google.com/').then(res => res.text()).then(body => console.log('success'));
^
TypeError: fetch is not a function
...
Workaround
Seems to work with both module load formats in node-fetch 1.7.3
Reacted by Alexey Sh, Jake Stoeffler, Lonnie Best, Tony Edwards, Luís, ajhool, wilensky, Michel Bunschoten, Vasyl Zuziak, Cheng Zheng(郑诚) and 10 more
I tried looking through the node-fetch issues before filing this and didn't see anyone with the same problem.
I can have a look at this tomorrow if you want a pull request. I thought I'd post here so that people who are googling for answers can find it. It seems to be something that broke recently because it works in 1.7.3.
Reacted by Lonnie Best, Tejas Suthar, Yassin Youssouf, stefanpl and João Koritar
Can you first explain what exactly you are doing with webpack if you are NOT bundling it for frontend?
Your issue has no actual error from webpack, so how are we going to troubleshoot this, as node-fetch should have no knowledge of the bundler you want to use.
Reacted by Steven Wright, Rosie, Šimon Demočko, Matthias Vonend, Dom, jlangree, James Berry, Radu Costea, kartikdutt-msft, John Weisz and 7 more
All of the same benefits that you get from bundling in frontend dev apply equally to writing console and server-side code i.e. you can use the latest javascript language features without having to worry about the environment within which your code is deployed and you can ship a complete app.bundle.js which requires no npm install. Try doing some AWS Lambda dev if you want to see the benefits of using a bundler for node code.
I realise that maintaining a library must be a huge headache given the state of javascript ecosystem. I'll see what I can do today to (1) fix the problem (2) come up with a unit test. However please appreciate that (1) is more my concern than (2) and I already have a workaround for (1) which is to use an older version of your library (i.e. v 1.7.3)
Reacted by Sean Yu, Willian Carvalho, Ari Palo, Elecweb, Steven Wright, Paloma, Cristian Szwarc and Satvik Sharma
Seems like this is an evolving area of node development - see here and here. I'm just going to stick on 1.7.3 for now - I thought there would be a quicker fix.
Reacted by Medin, Brandon Bradley, Ben Larson and Hosana Gomes
Reacted by Frank Blaauw, Ari Autio, Valérian, Rocco Musolino, Dmitri Dimitrioglo, Clinton Chau, Ricardoj626, DSnopov, Thomas BERTET, Damien Simonin Feugas and 44 more
Reacted by Lonnie Best, wilensky, Vecheka, Mayowa Daniel, Sean Devonport, Hunter Garrett, cbm-imouandjolobe, Charles Lewis, Michael, Tanner Dolby and 3 more
Reacted by Lonnie Best, wilensky, Vecheka, Sean Devonport, Hunter Garrett, cbm-imouandjolobe, Tomer, Nate Simonsen, Ziu and timsaunders-dev
Came across this issue, and found the corresponding discussion on the webpack issue tracker: webpack/webpack#4742
It has some workarounds for library authors that might be relevant. I don't have enough context to know if a workaround on the node-fetch side is the right way to go (or which workaround to use), so I didn't make a PR.
Is not a workaround, it is what export default means in commonJS terms. And, on the contrary const fetch = require('node-fetch') would translate to import * as fetch from "node-fetch" in ESM terms. the problem is not webpacks translation, (every transpiler will transpile export default X into module.exports.default = X, so importing with require().default is correct). The problem is assuming that require === import, which it's not, it's not syntactic sugar, they literally mean different things.
If @ilanc is bundling anyway, he could just use import instead of require?
Edit; note that module.exports = X (which would enable const X = require()) does not have an equivalent in ESM! Which one might love or hate, but is not something webpack can be blamed for.
Reacted by Satyajit Sahoo, swyx.io, Alex Bratsos, Quentin Roy, Sean Yu, Tobias Løfgren, Jason, Alexandre Lorret, Cody Hoffman and T
Now Webpack uses the same resolve algorithm as Node, and will pick up the CommonJS variant of node-fetch. Webpack does warn about the optional encoding module. If you prefer you can hide that warning using this config in webpack.config.js:
stats: {warningsFilter: "Module not found: Error: Can't resolve 'encoding'"},
Reacted by Pål Brattberg, Ari Palo, Artem Bass, greyson, Alexey P., Christian, lcondon, Luke McQuade and Philipp
On May 22, 2019, at 1:02, Bob van Manen ***@***.***> wrote:
The problem is that Webpack favors modules over CommonJS by default. This is different from Node which results in mixing import and require.
node-fetch provides 2 flavors: CommonJS, and Modules: https://github.com/bitinn/node-fetch/blob/master/package.json#L5-L7
When Node sees require("node-fetch), it'll pick the main config while Webpack picks module. You can reconfigure this in webpack.config.js:
resolve: {
mainFields: ["main"],
},
This still doesn't work though, because main references lib/index and the first extension that Webpack uses is .mjs. That can be changed as well:
resolve: {
extensions: [".js"],
mainFields: ["main"],
},
Now Webpack uses the same resolve algorithm as Node, and will pick up the CommonJS variant of node-fetch. Webpack does warn about the optional encoding module. If you prefer you can hide that warning using this config in webpack.config.js:
stats: {
warningsFilter: "Module not found: Error: Can't resolve 'encoding'"
},
—
You are receiving this because you modified the open/close state.
Reply to this email directly, view it on GitHub, or mute the thread.
With node-fetch 2.1.2 you have to use the ES6 module import form:
import fetch from 'node-fetch'
If you use the commonjs form:
const fetch = require('node-fetch')
then the resulting bundle won't work properly:
node-fetch-webpack/bundle.js:103
fetch('https://google.com/').then(res => res.text()).then(body => console.log('success'));
^
TypeError: fetch is not a function
...
Workaround
Seems to work with both module load formats in node-fetch 1.7.3
if webpack is used in the project, global.fetch won't work. Add following library in app root level.( recommended for serverless-webpack / any other web pack for lambda codestart )
If you're using this within a node_module you're maintaining, falling back to require("fetch").default if it's not undefined may be a good approach for you
Activity
bitinn commentedon May 6, 2018
If you webpack
node-fetch, you literally just get this:https://github.com/bitinn/node-fetch/blob/master/browser.js
It would be great for people to inform us: what does webpack want? Why do we keep running into this sort of problems with webpack?
ref: https://github.com/bitinn/node-fetch/search?q=webpack&type=Issues
ilanc commentedon May 6, 2018
Hi @bitinn you need to add the
target: 'node'to the webpack config (e.g. see https://github.com/ilanc/node-fetch-webpack/blob/master/webpack.config.js) otherwise it defaults to webpacking for the web (i.e. a browser). There are also babel settings to consider as well (e.g. babel-preset-env, .babelrc).I tried looking through the node-fetch issues before filing this and didn't see anyone with the same problem.
I can have a look at this tomorrow if you want a pull request. I thought I'd post here so that people who are googling for answers can find it. It seems to be something that broke recently because it works in 1.7.3.
bitinn commentedon May 6, 2018
Can you first explain what exactly you are doing with webpack if you are NOT bundling it for frontend?
Your issue has no actual error from webpack, so how are we going to troubleshoot this, as node-fetch should have no knowledge of the bundler you want to use.
ilanc commentedon May 7, 2018
All of the same benefits that you get from bundling in frontend dev apply equally to writing console and server-side code i.e. you can use the latest javascript language features without having to worry about the environment within which your code is deployed and you can ship a complete app.bundle.js which requires no npm install. Try doing some AWS Lambda dev if you want to see the benefits of using a bundler for node code.
I realise that maintaining a library must be a huge headache given the state of javascript ecosystem. I'll see what I can do today to (1) fix the problem (2) come up with a unit test. However please appreciate that (1) is more my concern than (2) and I already have a workaround for (1) which is to use an older version of your library (i.e. v 1.7.3)
ilanc commentedon May 7, 2018
Hmmm the problem is the mixing of module types. 1.7.3 has commonjs and 2.1.2 has es6 modules:
Seems like this is an evolving area of node development - see here and here. I'm just going to stick on 1.7.3 for now - I thought there would be a quicker fix.
bitinn commentedon May 7, 2018
I am going to close this as we still don't know why webpack doesn't take
export default function, which it should.If webpack expect ALL backend modules to add their config file then they are badly mistaken (which I believe they are smart enough to avoid).
Your issue is more on how to use webpack properly, than node-fetch having a "bug".
Of course, if people got a good suggestion on why and how to mitigate this, then we may at least update our readme to give some pointers.
ilanc commentedon May 7, 2018
I thought about this at lunch. There's another workaround:
This is probably the best workaround because it supports:
node app.jssee ilanc/node-fetch-webpack@3632977
TimothyJones commentedon Jan 8, 2019
Came across this issue, and found the corresponding discussion on the webpack issue tracker: webpack/webpack#4742
It has some workarounds for library authors that might be relevant. I don't have enough context to know if a workaround on the
node-fetchside is the right way to go (or which workaround to use), so I didn't make a PR.bitinn commentedon Jan 8, 2019
@TimothyJones Thx for linking this issue.
But it's not an easy fix to just dump
modulefield, the source of problem is webpack, notnode-fetch, to be honest.mweststrate commentedon Feb 12, 2019
Is not a workaround, it is what
export defaultmeans in commonJS terms. And, on the contraryconst fetch = require('node-fetch')would translate toimport * as fetch from "node-fetch"in ESM terms. the problem is not webpacks translation, (every transpiler will transpileexport default Xintomodule.exports.default = X, so importing withrequire().defaultis correct). The problem is assuming thatrequire === import, which it's not, it's not syntactic sugar, they literally mean different things.If @ilanc is bundling anyway, he could just use
importinstead of require?Edit; note that
module.exports = X(which would enableconst X = require()) does not have an equivalent in ESM! Which one might love or hate, but is not something webpack can be blamed for.Fix missing node-fetch when building using webpack
Fix node-fetch import
bobvanmanen commentedon May 21, 2019
The problem is that Webpack favors modules over CommonJS by default. This is different from Node which results in mixing
importandrequire.node-fetch provides 2 flavors: CommonJS, and Modules: https://github.com/bitinn/node-fetch/blob/master/package.json#L5-L7
When Node sees
require("node-fetch), it'll pick themainconfig while Webpack picksmodule. You can reconfigure this inwebpack.config.js:This still doesn't work though, because
mainreferenceslib/indexand the first extension that Webpack uses is.mjs. That can be changed as well:Now Webpack uses the same resolve algorithm as Node, and will pick up the CommonJS variant of node-fetch. Webpack does warn about the optional
encodingmodule. If you prefer you can hide that warning using this config inwebpack.config.js:bitinn commentedon May 21, 2019
MickL commentedon Feb 14, 2020
import fetch from 'node-fetch'worked for me. Please update the readme.Richienb commentedon Feb 15, 2020
v3 may have fixed this since we're using Pika which should have specifically fixed this problem.
webpack issue
tamjidtech commentedon May 13, 2020
save my day, thanks
ezhilarasan446 commentedon Aug 13, 2020
if webpack is used in the project, global.fetch won't work. Add following library in app root level.( recommended for serverless-webpack / any other web pack for lambda codestart )
require('cross-fetch/polyfill');Add workaround to fix node-fetch issue node-fetch/node-fetch#450
Use .default for compatibility with webpack
Add workaround to fix node-fetch issue node-fetch/node-fetch#450
Add workaround to fix node-fetch issue node-fetch/node-fetch#450
fallback to require('node-fetch').default due to node-fetch/node-fetc…
kyle-ssg commentedon Jan 13, 2022
If you're using this within a node_module you're maintaining, falling back to require("fetch").default if it's not undefined may be a good approach for you
https://github.com/Flagsmith/flagsmith-nodejs-client/pull/21/files#diff-537b33488a6027f3f9f309df24d9af4c6819c635cdae1d650245a259e4f8e335R1