1

I am using js2-mode and comint-mode to run the Node REPL in emacs. For some reason the prompt is returning a lot of undefined and ...undefined responses, which it wouldn't do normally. for instance, this code:

function run () {
    console.log('hello there');
}
function next () {
    console.log('hello again');
}

run();
next();

Prints out the following:

... ... undefined                                                                       
> undefined                                                                                                                                                                ... ... undefined                                                                                                                                                         > undefined  
> hello there                                                                           
undefined                                                                               
> hello again                                                                                                                                                                undefined                                                                               
  • Why not? The results make sense to me. A function declaration doesn't produce a value, and the JS way of saying that is to say that the value is undefined. Then, after you execute run, it still doesn't return anything, which is again, the "sort of" unit type, i.e. undefined. – wvxvw Apr 14 '15 at 21:29
  • It makes sense, sure. But I doubt we seeing a bunch of undefineds is very helpful or pretty. – PythonNut Apr 15 '15 at 17:47
  • Additionally, the first line, when actually returning a value has the ... ... value which is strange. – Startec Apr 15 '15 at 23:31
  • alos @wvxvw - by that logic there should actually be another undefined as next doesn't return anything. – Startec Apr 25 '15 at 2:11
  • The ellipsis must be a residue from indentation. I can't tell for sure. I'm certain though you'll get more of these printed when you copy-and-paste a larger block of code into the REPL. And, yes, I'd imagine there to be one more undefined. I would maybe try looking into Node's implementation of REPL, perhaps there are even ways to control it... – wvxvw Apr 25 '15 at 7:33
1

After many attempts to get rid of undefined... I ended up using a shell command and send all my Javascript to node and return just the result instead of a console.

If you are interested here is the code.

(define-key js-mode-map (kbd "C-c C-c")
    #'(lambda ()  
       (interactive)
       (shell-command-on-region (point-min) (point-max) "node")))
  • you put this in your .emacs file? – Startec Apr 28 '15 at 21:33
  • Yes you can, depending on how you organize your code. – surya May 1 '15 at 11:50
0

with js-comint when i did js-load-file with the above code snippet, it printed three lines -

hello there
hello again
{}

Why don't try running the code as a batch script. In interpreted mode, any js engine is likely to print undefined on every statement execution.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.