Node.js Uncaught TypeError: callback is not a function in process.nextTick

skeggse

I'm getting the following error. It's clearly coming from a callback passed to process.nextTick. Given that the stack trace is virtually unusable, how do I debug this? What's going on behind the scenes, and how can I resolve this in a larger project?

TypeError: callback is not a function
    at nextTickCallbackWith0Args (node.js:420:9)
    at process._tickDomainCallback (node.js:390:13)

N.B. Older versions of Node had a different implementation of process.nextTick, and spit out the following stack trace.

Uncaught TypeError: undefined is not a function
    at process._tickCallback (node.js:415:13)
skeggse

Node.js tick callback prefers to throw an error if the function you pass to process.nextTick is not a function after a tick has passed, rather than telling you when you first call it.

Where a function will just get called after a tick:

// this will print "hi!", followed by "hello there"
process.nextTick(function() {
  console.log('hello there');
});
console.log('hi!');

A non-function won't actually matter until the next tick:

// this will just print "hi!", and an error will be thrown later on
process.nextTick(undefined);
console.log('hi!');

Below, I'll outline two diagnostic tools you can use to track down the culprit.

Tool 1: longjohn

As it turns out, this kind of scenario is common enough that a tool exists to extend stack traces with information from previous ticks.

Simply install longjohn (available on npm), and require it when your process starts. This will yield a stack trace similar to the following:

TypeError: Cannot read property 'apply' of undefined
    at nextTickCallbackWith0Args (node.js:420:9)
    at process._tickCallback (node.js:349:13)
    at Function.Module.runMain (module.js:443:11)
    at startup (node.js:139:18)
    at node.js:968:3
---------------------------------------------
    at Object.<anonymous> (.../example.js:3:9)
    at Module._compile (module.js:409:26)
    at Object.Module._extensions..js (module.js:416:10)
    at Module.load (module.js:343:32)
    at Function.Module._load (module.js:300:12)
    at Function.Module.runMain (module.js:441:10)
    at startup (node.js:139:18)
    at node.js:968:3

Now that you have a useful stack trace, you can use your debugging skills to figure out why you're giving process.nextTick undefined rather than a function.

Monkey patching

Because javascript is dynamic, we can easily setup a new version of process.nextTick (that is, we can monkey patch it) which traces the call to it, but proxies to the real nextTick function:

var nextTick = process.nextTick;

process.nextTick = function(callback) {
  if (typeof callback !== 'function') {
    console.trace(typeof callback + ' is not a function');
  }
  return nextTick.apply(process, arguments);
};

Note: I don't recommend doing this for production code, but it does make it much easier to locate where you're providing something besides a function to process.nextTick. Remember that this is modifying the global process variable, so it might break at some point and it's certainly not considered best practice, but it'll do nicely for finding small problems. (Addendum: and since I originally wrote this post, I've had to revise it because it broke once.)

Trace: undefined is not a function
    at process.nextTick (.../example.js:5:13)
    at Object.<anonymous> (.../example.js:10:9)
    at Module._compile (module.js:409:26)
    at Object.Module._extensions..js (module.js:416:10)
    at Module.load (module.js:343:32)
    at Function.Module._load (module.js:300:12)
    at Function.Module.runMain (module.js:441:10)
    at startup (node.js:139:18)
    at node.js:968:3

As above, now that you have a useful stack trace, you can identify the root cause.

In terms of the internals, check out the _tickCallback function in node.js in the source (old link).

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Node.js Uncaught TypeError: callback is not a function in process.nextTick

From Dev

Node.js asynchronous function, process.nextTick()

From Dev

Node.js - process.nextTick and parseJSON

From Dev

Creating a waiting function using process.nextTick or setImmediate - node.js

From Dev

How to pass function/callback to child process in Node.js?

From Dev

Node server response error: process.nextTick(function(){throw err;});

From Dev

TypeError: callback.apply is not a function (Node.js & Mongodb)

From Dev

Uncaught TypeError: callback.apply is not a function

From Dev

MongoDB Database Semaphores and Node.js Process.NextTick()

From Dev

Significance of using setImmediate() and process.nextTick() in Node.js

From Dev

node js callback function

From Dev

node js callback() function is not defined?

From Dev

Node fs rmdir TypeError 'Callback must be a function'

From Dev

node.js callback of an export typeerror undefined

From Dev

Uncaught TypeError: undefined is not a function - typeahead.js

From Dev

Ember.js: Uncaught TypeError: undefined is not a function

From Dev

Uncaught TypeError: undefined is not a function - typeahead.js

From Dev

BigVideo.js - Uncaught TypeError: undefined is not a function

From Dev

Newcomer to Node, how to understand process.nextTick()

From Dev

Understanding Node.js event loop. process.nextTick() never invoked. Why?

From Dev

Node.js process.nextTick still blocking server from getting requests

From Dev

Node.js — Maximum call stack size exceeded, even with process.nextTick()

From Dev

passport.js and process.nextTick in strategy

From Dev

passport.js and process.nextTick in strategy

From Dev

TypeError: Object is not a function Node JS

From Dev

TypeError: undefined is not a function Node JS

From Dev

TypeError: undefined is not a function in node js

From Dev

typeerror undefined is not a function in node js

From Dev

knockout.js Uncaught TypeError: Unable to process binding

Related Related

  1. 1

    Node.js Uncaught TypeError: callback is not a function in process.nextTick

  2. 2

    Node.js asynchronous function, process.nextTick()

  3. 3

    Node.js - process.nextTick and parseJSON

  4. 4

    Creating a waiting function using process.nextTick or setImmediate - node.js

  5. 5

    How to pass function/callback to child process in Node.js?

  6. 6

    Node server response error: process.nextTick(function(){throw err;});

  7. 7

    TypeError: callback.apply is not a function (Node.js & Mongodb)

  8. 8

    Uncaught TypeError: callback.apply is not a function

  9. 9

    MongoDB Database Semaphores and Node.js Process.NextTick()

  10. 10

    Significance of using setImmediate() and process.nextTick() in Node.js

  11. 11

    node js callback function

  12. 12

    node js callback() function is not defined?

  13. 13

    Node fs rmdir TypeError 'Callback must be a function'

  14. 14

    node.js callback of an export typeerror undefined

  15. 15

    Uncaught TypeError: undefined is not a function - typeahead.js

  16. 16

    Ember.js: Uncaught TypeError: undefined is not a function

  17. 17

    Uncaught TypeError: undefined is not a function - typeahead.js

  18. 18

    BigVideo.js - Uncaught TypeError: undefined is not a function

  19. 19

    Newcomer to Node, how to understand process.nextTick()

  20. 20

    Understanding Node.js event loop. process.nextTick() never invoked. Why?

  21. 21

    Node.js process.nextTick still blocking server from getting requests

  22. 22

    Node.js — Maximum call stack size exceeded, even with process.nextTick()

  23. 23

    passport.js and process.nextTick in strategy

  24. 24

    passport.js and process.nextTick in strategy

  25. 25

    TypeError: Object is not a function Node JS

  26. 26

    TypeError: undefined is not a function Node JS

  27. 27

    TypeError: undefined is not a function in node js

  28. 28

    typeerror undefined is not a function in node js

  29. 29

    knockout.js Uncaught TypeError: Unable to process binding

HotTag

Archive