Common Errors: "X is not a function" errors

Join the AI Workshop to learn more about AI and how it can be applied to web development. Next cohort February 1st, 2026

The AI-first Web Development BOOTCAMP cohort starts February 24th, 2026. 10 weeks of intensive training and hands-on projects.


The “is not a function” error is one of the most common JavaScript errors. Let’s explore when it happens and how to fix it.

The semicolon issue

If you write JavaScript without semicolons, you might encounter this error:

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

This happens when JavaScript misinterprets your code. Consider this example:

const fs = require('fs')

(async () => {
  //...
})()

JavaScript doesn’t see a semicolon after require(), and we start a line with a (. JS thinks we’re trying to execute a function, considering require('fs') as the function name.

The fix

Add a semicolon. Either at the end of the previous line:

const fs = require('fs');

(async () => {
  //...
})()

Or at the beginning of the IIFE:

const fs = require('fs')

;(async () => {
  //...
})()

Tip: Top-level await is now a thing in modern JavaScript, so you can use await directly at the top level instead of wrapping it in an IIFE.

The cb.apply is not a function error

Another variant is cb.apply is not a function, which can occur with certain npm packages after a Node.js version upgrade.

This typically happens with packages like graceful-fs when running older tools. If you encounter this:

  1. Check if the tool you’re using has an update
  2. Consider using a Node version manager to switch to a compatible Node.js version
  3. As a last resort, you can patch the problematic file by commenting out the offending code

For example, in graceful-fs/polyfills.js, you might need to comment out:

// fs.stat = statFix(fs.stat)
// fs.fstat = statFix(fs.fstat)
// fs.lstat = statFix(fs.lstat)

General causes

The “is not a function” error generally occurs when:

  1. You’re trying to call something that isn’t a function
  2. A variable you expected to be a function is actually undefined or another type
  3. You forgot to import a function
  4. Semicolon-less code is being misinterpreted

Lessons in this unit:

0: Introduction
1: ▶︎ "X is not a function" errors
2: "document is not defined" error
3: Cannot assign to read only property
4: Parse failure: Unterminated string constant
5: FormData multipart fetch issues
6: regeneratorRuntime is not defined
7: Unexpected identifier in modules
8: Unterminated string literal