Microsoft Graph SDK ` is not a function`

The problem

This will be a quick one today. I recently came across this issue when running an (Node.js) application using the MS Graph JavaScript SDK in production (fine in development).

This is most likely a weird edge case in my configuration or how I built my application, but wanted to put it out there in case anyone else has this issue.

Whenever I was calling a Graph endpoint it was returning an error message body: is not a function with a status code of statusCode: -1, which didn’t give me a lot to go off of!

During development of the application I had not noticed this as it only appeared when running production (transpiled, bundled, minified) code, which is why I assume the error is so generic.

As it was only happening with production code, I first set about looking at the build config file (Webpack) and could not fix it this way (not saying there isn’t a way to fix it). Next I tried different versions of the SDK and modifying my code, but nothing worked…

The fix

…Until I re-read the SDK documentation and that it uses fetch under the hood to make the endpoint calls and you may require an additional package.

As you most probably know fetch is used to make HTTP calls in Javascript and it is usually bundled with most browsers, but not with Node.js until very recent versions (which aren’t supported by the SDK). Because of this, you sometimes require an additional package to be installed when using the SDK to add support for fetch, but it is not a bundled package; you need to install it yourself manually.

The documentation has listed three recommended packages to choose from:

  • isomorphic-fetch
  • cross-fetch
  • whatwg-fetch

I want to say that originally, only isomophic-fetch was recommended, which is what I have always used. I was clutching at straws to work our this problem so searched for something along the lines of “webpack isomorphic-fetch call is not a function” and came across a GitHub issue that seemed very similar.

There looked to be workarounds, but also mention of using cross-fetch instead. Which is what I ended up trying, and holy smokes, it works 🎉. Turns out its not the SDK after all, just the additional package!

Hope this helps someone out there!