Open kevzettler opened 3 years ago
facing the same problem
tested on version 2.2.1 and version 3.0.0 and I can confirm it is not happening on version 2.2.1, only on version 3.0.0
I think it's a bug from node-fetch
because it tagged itself as module
see here so webpack load its .mjs instead of .js despite their exports have different semantics(scroll former two links to bottom to see the differences)
I guess you may in a isomorphic case, if so try to indicate webpack that node-fetch
should be treated as it's commonjs module via webpack.config.js
:
module.exports = {
// ...
externals: {
'node-fetch': 'commonjs2 node-fetch',
}
};
I ended up switching to cross-fetch
which seems to work in the same way without the same issue!
Same issue here. We've switched to cross-fetch
as well which solves the issue for us.
FYI we've made it work with isomorphic-fetch
with the following configuration but we thought it was too brittle to keep:
module.exports = {
// ...
resolve: {
// ...
mainFields: ['main', 'module']
}
}
With our target
setting set to node
the default for mainFields
was ['module', 'main']
.
We've also tried to made it per-dependency for both node-fetch
and/or isomorphic-fetch
by doing the following but this one did not work:
module.exports = {
// ...
resolve: {
// ...
byDependency: {
'node-fetch': {
mainFields: ['main', 'module']
},
'isomorphic-fetch': {
mainFields: ['main', 'module']
}
}
}
}
Now that NodeJS have built in fetch in v18+ then i think you should be using it instead.
now more dependency is needed any more.
got it working using webpack alias
alias: {
'node-fetch': path.join(
cwd,
'../../node_modules/node-fetch/lib/index.js',
),
},
I think it's a bug from
node-fetch
because it tagged itself asmodule
see here so webpack load its .mjs instead of .js despite their exports have different semantics(scroll former two links to bottom to see the differences)I guess you may in a isomorphic case, if so try to indicate webpack that
node-fetch
should be treated as it's commonjs module viawebpack.config.js
:module.exports = { // ... externals: { 'node-fetch': 'commonjs2 node-fetch', } };
Just FYI for anyone else running into this, commonjs2
was a Webpack 4 externals type, if you're using Webpack 5 you'll want to specify node-commonjs
. I.e.:
module.exports = {
// ...
externals: {
'node-fetch': 'node-commonjs node-fetch',
}
};
Trying to use this in a node.js worker and seeing error
realFetch.call is not a function