leeand00 leeand00 - 4 days ago 5
Javascript Question

Why can't I eval a .load to start an interactive script in node.js?

In node.js you can pass the argument

-i
to get an interactive console, and then pass
-e
to evaluate a javascript statement.

I tried running:

$ node -i -e '.load ./someScript.js'
.load someScript.js;
^

SyntaxError: Unexpected token .
at Object.exports.runInThisContext (vm.js:53:16)
at Object.<anonymous> ([eval]-wrapper:6:22)
at Module._compile (module.js:410:26)
at node.js:578:27
at nextTickCallbackWith0Args (node.js:419:9)
at process._tickCallback (node.js:348:13)


And I get an error, but if I try to run the same thing from the interactive node prompt, it loads just fine; i.e.

> .load ./someScript.js


Is there something else I need to do to make this work?

Answer

Why can't I eval a .load to start an interactive script in node.js?

Because -e is for evaluating JavaScript code. The interactive REPL's commands aren't JavaScript, they're REPL interactive commands.

This question asks how to go interactive after running a script, which doesn't answer the "why" part (that's why I've posted the above), but its answers may give you some options for the somewhat-implied "...and what do I do instead?" part. :-)

Comments