Node.js 的命令行参数

command line arguments to Node.js

我在这里阅读了几个相关问题,但仍然无法弄清楚我做错了什么。 这是我的脚本 (simple.js):

    var x = process.argv[2]
    console.log(x);

现在当我输入 REPL

    .load simple.js Hello

REPL 回答

 Failed to load:simple.js Hello

但我已经做到了 (simple2.js):

    console.log(process.argv);

当我在 REPL 中加载这个文件时,它会回答

    > console.log(process.argv);
    [ 'C:\Program Files\nodejs\node.exe' ]
    undefined

您不能将命令行参数传递给 .load。第二个案例没有失败,因为你没有通过

如果你想使用 cmdline args,只需使用 OS 终端并输入 node simple.js Hello.

.load 在 Node REPL 中只加载一个文件。从 REPL docs 看来 .load 的主要用例是恢复使用 .save.

保存的 REPL 会话

在您的成功示例中,它只是显示当您在终端运行 node

中设置的process.argv