[英]How can I check if an import statement is working correctly in the Node 6 REPL?
[英]How can I make the Node REPL print the result only?
我想使用.load myFile.js
運行腳本,但我不想打印文件中的所有內容,只打印文件的值/結果。 我在我的項目中使用 Electron、xterm.js 和 node-pty。
例如,使用此代碼:
// type your code here
function sum(x, y) {
return x + y
}
sum(2, 3)
我得到這樣的東西:
> // type your code here
undefined
> function sum(x, y) {
... return x + y
... }
undefined
> sum(2, 3)
5
>
如果您對當前范圍內運行的代碼沒問題,只需對其進行eval
:
> eval(fs.readFileSync('myFile.js', 'utf8'))
5
如果你想要一個新的范圍, vm.runInNewContext
(這不是安全問題,只是范圍清潔):
> vm.runInNewContext(fs.readFileSync('myFile.js', 'utf8'))
5
像require
這樣的全局變量可以在它的第二個參數中傳遞。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.