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; ie
> .load ./someScript.js
Is there something else I need to do to make this work?
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. :-)
You could maybe use a custom script to initiate the REPL from its module :
Something like:
const repl = require('repl'),
path = require('path'),
location = process.argv[2],
base = path.basename(location),
clean = path.split('.')[0];
const r = repl.start('> ');
Object.defineProperty(r.context, clean, {
configurable: false,
enumerable: true,
value: require(location)
});
So, now you can do node loadModule /path/to/load.js
, the module will be available depending on the base of the path ( /path/to/load.js
will be available under load
for example)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.