如何使用 Node.js repl 以文件作为输入
How to use Node.js repl with file as input
我希望将 js
文件输入用作 node.js repl
,就好像它是命令行代码一样:
node -e '<my code>'
,但使用 <filename>
而不是内联代码。
如何将文件名作为输入,而不需要使用另一个文件(例如fs.readFileSync(..)
)
一种方法是使用 REPL 的 .load
command 和 Node 的 -i
标志。
echo ".load test.js" | node -i
输出:
$ echo ".load test.js" | node -i
Welcome to Node.js v12.8.1.
Type ".help" for more information.
> .load test.js
2 + 2;
4
我希望将 js
文件输入用作 node.js repl
,就好像它是命令行代码一样:
node -e '<my code>'
,但使用 <filename>
而不是内联代码。
如何将文件名作为输入,而不需要使用另一个文件(例如fs.readFileSync(..)
)
一种方法是使用 REPL 的 .load
command 和 Node 的 -i
标志。
echo ".load test.js" | node -i
输出:
$ echo ".load test.js" | node -i
Welcome to Node.js v12.8.1.
Type ".help" for more information.
> .load test.js
2 + 2;
4