56

Recently I installed node.js on my Windows 7 machine.

On execution of JavaScript, I get an undefined message along with successful execution of the expression.

What's wrong here? I have not noticed any other side effects.

enter image description here

Octavian Helm
  • 39,405
  • 19
  • 98
  • 102
Sandeep G B
  • 3,957
  • 4
  • 26
  • 43
  • Does this answer your question? [node.js REPL "undefined"](https://stackoverflow.com/questions/10186565/node-js-repl-undefined) – Flash Ang Nov 14 '20 at 09:06

10 Answers10

53

The JavaScript functions always return something. If you don't specify something to return in the function, 'undefined' is returned by default (you can check this out in Firebug too).

Don't worry though, this doesn't affect anything, you can ignore it.

alessioalex
  • 62,577
  • 16
  • 155
  • 122
  • 1
    +1. Thanks for the reply. Is there a way to get rid of 'undefined', what should be the syntax? – Sandeep G B Dec 10 '11 at 15:20
  • 1
    If you make a program (web app or command line), you'll never get "flying" undefineds, so don't worry about that. – alessioalex Dec 10 '11 at 15:22
  • When I don't enter any function or any character; then also "undefined" is displayed. Can you please explain what is happening under the hood? – Sandeep G B Dec 10 '11 at 15:30
  • 6
    What's happening under the hood. Enter asdasd for example and see this error: at REPLServer.eval (repl.js:80:21) ... So behind the scenes there's an eval() function which returns 'undefined'. – alessioalex Dec 10 '11 at 15:35
  • Thanks. Got it now :) I should start looking at how things are implemented and not just how to get it working. – Sandeep G B Dec 10 '11 at 16:27
36

Just write "hello world"; and hit enter... it will return "hello world" instead of undefined, thus no undefined is displayed. console.log returns undefined and also logs arguments to console so you get multiple messages.

Esailija
  • 138,174
  • 23
  • 272
  • 326
  • Thanks Esailija. When I don't enter any function or any character; then also "undefined" is displayed. Can you please explain what is happening under the hood? – Sandeep G B Dec 10 '11 at 15:28
  • @SandeepGB, what do you expect for not having any input? :P The return value of not entering anything makes most sense to be `undefined` – Esailija Dec 10 '11 at 15:32
  • 2
    I was trying to understand whats going on. alessioalex answered my question :) – Sandeep G B Dec 10 '11 at 16:26
19

As pointed out by others, javascript function will always return undefined if you do not specify any return value. You can just ignore it. It's not going to cause any harm. But if it's annoying you too much then you can turn it off in repl. Repl has this property ignoreUndefined which is set to false by default. You can set it to true. Try this:

module.exports.repl.ignoreUndefined = true;
AvinashB
  • 191
  • 1
  • 3
9

Well, the question was made some years ago, but there is still another way to explain what is happening here.

Try next command:

console.log("Hello World") || "Bye World";

As mentioned function console.log() returns undefined normally and you can choose a better return value. Since Non-strict (abstract) comparison considers undefined equal to false the || operator allows you that choice.

Considering that it is just a debugging tool, this is unnecessary in general use, but helps to understand that console displays text sent to stdout and also evals the command and displays the returned value, or undefined if no value was received.

Melo Waste
  • 151
  • 1
  • 8
4

If you need to turn on ignoreUndefined from inside a module, you can use the following line to simulate AvinashB's answer without typing into the console by yourself:

process.stdin.emit('data', "module.exports.repl.ignoreUndefined = true;\n");
Community
  • 1
  • 1
RienNeVaPlu͢s
  • 7,442
  • 6
  • 43
  • 77
4

node also prints undefined because IN GENERAL, it displays the return value of each command and console.log doesn't return anything.

Zameer Ansari
  • 28,977
  • 24
  • 140
  • 219
0

this is happening because console.log has (null/undefined) return type to solve this issue.. have a function returning a string.. then log that function returned value... that will overcome the undefined issue..

const addNotes= function(msg)
{
    return msg;
}
console.log(addNotes('say hello'));
Mahmoud Sayed
  • 151
  • 2
  • 10
0

Just ignore it, no need to worry at all. I too had the same issue when I called the function without any return type. So node used to print undefined return type for the called function.

function emitEvent(eventname){
    eventEmitter.emit(SIV_EVENT);
}
console.log(emitEvent());

Check undefined here, https://nodejs.org/api/util.html.

0

To avoid seeing undefined output from commands when using Node's repl module, set ignoreUndefined to true in repl.start.

Ken Lin
  • 1,819
  • 21
  • 21
0

Use middle where in your index file

app.use(express.json())