1

In normal Javascript in the Browser Google Chrome will display an error in the console when you try to use an undefined variable. However in Node.js no error is displayed and I think the function that is using the undefined variable exits.

I've added:

process.on('uncaughtException', function(err) {
    console.log( 'uncaughtException :', err );
});

but this isn't executed.

I've also added try-catch in the calling function and no exception is raised there.

And I'm using: "use strict";

So my question is, is there some way I can get/see an error when an undefined variable is accessed?

1
  • 1
    Cannot confirm your observation, I've executed a demo in the REPL and in a file, with and without "use strict";. The expected behaviour is that node exits, logging this message to the console. Do you have any other relevant code? Commented Feb 15, 2012 at 21:44

2 Answers 2

1

Node will do this automatically.

// test.js
"use strict";

function badFunction() {
    return iDontExist + 3;
}

badFunction();

Then:

C:\Users\Domenic\Programming>node test.js

node.js:201
        throw e; // process.nextTick error, or 'error' event on first tick
              ^
ReferenceError: iDontExist is not defined
    at badFunction (C:\Users\Domenic\Programming\test.js:4:12)
    at Object.<anonymous> (C:\Users\Domenic\Programming\test.js:7:1)
    at Module._compile (module.js:444:26)
    at Object..js (module.js:462:10)
    at Module.load (module.js:351:31)
    at Function._load (module.js:310:12)
    at Array.0 (module.js:482:10)
    at EventEmitter._tickCallback (node.js:192:40)
Sign up to request clarification or add additional context in comments.

3 Comments

Note that "use strict" isn't even necessary; recent versions of node do this by default.
Domenic & Rob W, thanks for your posts. I couldn't understand why I wasn't seeing the behaviour I expected and you indicate should occur. I tried Domenic's example and sure enough I see the exception, however in my code I don't.
Continued, I finally got around to installing node-inspector (on Windows) and an exception is thrown as expected, however it is being caught by code in connection.js line 311 which is part of a MongoDB driver. This is frustrating and annoying and explains why I'm having this issue. If I could somehow still see the error it would be ok.
0

The reason I'm not seeing this exception is other exception handler code is getting in before node. See my replies above re. the MongoDB driver.

Any other suggestions welcome. This is the code that is catching the exception:

          catch (err) {
            var errorObject = {err:"socketHandler", trace:err, bin:self.buffer, parseState:{
              sizeOfMessage:self.sizeOfMessage, 
              bytesRead:self.bytesRead,
              stubBuffer:self.stubBuffer}};
            if(self.logger != null && self.logger.doError) self.logger.error("parseError", errorObject);
            // We got a parse Error fire it off then keep going
            self.emit("parseError", errorObject, self);
          }              

Neville

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.