禁用此消息 - 现在禁用 _ 的表达式赋值?NodeJs-REPL.



当我使用var _ = require('underscore')时,我收到了这条消息Expression assignment to _ now disabled.

有什么方法可以用来避免此消息吗?

我可以更改变量名称,但我找到了具有相同节点的人,并且没有引发消息。

root@other:/# node
> var _ = require('underscore');
undefined
>
root@my:/# node
> var _ = require('underscore');
Expression assignment to _ now disabled.
undefined
>

因此,如果需要,您实际上可以定义自己的自定义 repl,文档在这里: https://nodejs.org/api/repl.html

例如,如果你想改变你正在描述的行为,你可以覆盖编写器函数以跳过该输出,或者只是(可能更容易(重新定义上下文变量本身:

const repl = require('repl');
const underscore = require('underscore');
const r = repl.start('> ');
Object.defineProperty(r.context, '_', {
configurable: false,
enumerable: true,
value: underscore
});

或者,如果您只想允许它而没有错误,只需执行他们所做的操作,但跳过错误消息:

Object.defineProperty(context, '_', {
configurable: true,
get: () => this.last,
set: (value) => {
this.last = value;
}
});

要实际使用上述内容,您需要运行包含它的脚本(如链接文档中所述(。这可以简单地通过

node myrepl.js

或者,如果您运行的是Linux或MacOS,则可以将其设置为可执行脚本并将其放在PATH中。

最新更新