Discord.js: ShardingManager: fetch is not a function

Created on 4 Nov 2019  Â·  7Comments  Â·  Source: discordjs/discord.js

Please describe the problem you are having in as much detail as possible:
Alright! Here we go. When I try to run my bot using the ShardingManager, I get this (linkified for reference):

(node:20052) UnhandledPromiseRejectionWarning: TypeError: fetch is not a function
at Function.fetchRecommendedShards
at ShardingManager.spawn

Note that node-fetch is imported correctly, as recommended by the node-fetch docs.

Include a reproducible code sample here, if possible:
My bot's open source. Here's the branch where I'm working on the update. The readme describes how to configure and provides a minimum viable configuration.

Further details:

  • discord.js version: 38d370fb18fdbf079b1c91793c0550e77667eccd (latest master)
  • Node.js version: 13.0.1
  • Operating system: Windows 10 Education Edition v1903, build 18362.418
  • Priority this issue should have – please be realistic and elaborate if possible: Pretty high. I can't be the only one using the ShardingManager, nor the only one having issues.
  • I use TypeScript and Webpack, neither of which complain when building.
unverified bug

Most helpful comment

This is why you don't use webpack for the backend, it has no purpose but makes everything a tad more complicated.

node-fetch, when used with webpack, just boils down to this https://github.com/bitinn/node-fetch/blob/master/browser.js

Fetch not being a global thing outside of browsers makes this whole thing blow up.

Edit:

There seems to be a workaround, but it would require using the default export in our code-base to alleviate webpack issues.

I don't see the point in doing this, esp if you are using Typescript there is no need to use webpack at all for your backend code, its utter nonsense.

All 7 comments

This is why you don't use webpack for the backend, it has no purpose but makes everything a tad more complicated.

node-fetch, when used with webpack, just boils down to this https://github.com/bitinn/node-fetch/blob/master/browser.js

Fetch not being a global thing outside of browsers makes this whole thing blow up.

Edit:

There seems to be a workaround, but it would require using the default export in our code-base to alleviate webpack issues.

I don't see the point in doing this, esp if you are using Typescript there is no need to use webpack at all for your backend code, its utter nonsense.

I use webpack because I build to deploy via Docker. This is a recent thing - it was working (and still is, using the same settings) on the 11.5.1 release on NPM.

You don't need webpack to deploy via Docker, if you would no one would use Docker.

Regardless of that, stable does not use node-fetch, it uses snekfetch a now deprecated http library.

I wouldn't consider a year, relatively "recent" either. #2587

"Last publish: 5 months ago."

I use webpack because I want to minimize image size. Regardless, in my config, Webpack is setup to build for Node targets.

The last publish of the v11 version, which is not what the master branch reflects.

The master branch is v12 with quite a few breaking changes, nothing to do with the last publish.

Again, this is a webpack issue. From researching this is the only thing I found that could help alleviate the problem on your side:
https://github.com/bitinn/node-fetch/issues/450#issuecomment-494475397

Ah. Thanks!

Was this page helpful?
0 / 5 - 0 ratings