node.js REPL 会话中来自 js 文件的未定义函数

问题描述 投票:0回答:1

我有一个测试 js 脚本要加载到 REPL 会话中:

// testRepl.js
const cls = () => console.clear();
const msg = txt => console.info(txt);
const ajax = url => msg(url);

我使用以下命令在 REPL 中加载此文件

> node
> .load .testRepl.js

问题在于REPL会话可以加载cls和msg函数,但无法识别ajax,如下图所示:

[![ajax 无法识别][1]][1]

所以这有效:

> cls()
> msg("Hello")

但这失败了:

> ajax("Hello World!")

**EDIT**

I am adding the screenshot of my testRepl.js:
[![testRepl.js][2]][2]


  [1]: https://i.stack.imgur.com/ISQcx.png
  [2]: https://i.stack.imgur.com/172N9.png
javascript node.js command-line-interface read-eval-print-loop
1个回答
0
投票

也许您的 .js 文件末尾没有换行符或其他内容,因为尝试重现该问题对我来说一切正常:

$ cat /tmp/foo.js
const cls = () => console.clear();
const msg = txt => console.info(txt);
const ajax = url => msg(url);

$ node
Welcome to Node.js v16.16.0.
Type ".help" for more information.
> .load /tmp/foo.js
const cls = () => console.clear();
const msg = txt => console.info(txt);
const ajax = url => msg(url);

undefined
> ajax
[Function: ajax]

在节点 v16、v18 和 v20 中尝试过。结果都是一样的。

© www.soinside.com 2019 - 2024. All rights reserved.