[英]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. 在node.js中,您可以传递参数
-i
以获得交互式控制台,然后传递-e
来评估javascript语句。
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?
为什么我不能评估.load以在node.js中启动交互式脚本?
Because -e
is for evaluating JavaScript code. 因为
-e
用于评估JavaScript代码。 The interactive REPL's commands aren't JavaScript, they're REPL interactive commands. 交互式REPL的命令不是JavaScript,而是REPL交互式命令。
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 : 您可以使用自定义脚本从其模块启动REPL:
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) 因此,现在您可以执行
node loadModule /path/to/load.js
,该模块将根据路径的基础而可用(例如, /path/to/load.js
node loadModule /path/to/load.js
将在load
下可用)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.