Code With Wolf


Fixing TypeError require(...) is not a function

Fixing TypeError: require(...) is not a function

I was coming across an odd error saying TypeError: require(...) is not a function when working on a very simple node.js script.

All I was trying to do was make an http request using the node-fetch package inside an invoked function.

Something like this ...

const fetch = require('node-fetch)

(async()={
const res = await fetch(SOME_URL);
const data = await res.json();
})();

When running the script, I got that TypeError.

What I discovered is that while I'm usually judicious about my ; use, I had left one out after the require statement.

After I added a semi-colon at the end of that line, I was any to run the script without an issue.

I actually believe the reason for this is explained in detail in another one of my blog post's here., and if you want to read a little more about what a TypeError is, check out this post.



© 2022 Code With Wolf