2017-10-05 67 views

回答

0

您可以添加任何JavaScript文件的開頭家當線,例如考慮這個文件,hello.js

#!/usr/bin/env node 

function hello(name) { 
    console.log('hello ' + name); 
} 

/* if called directly from command line or from a shell script */ 
if (require.main === module) { 
    hello(process.argv[2]); 
} 

module.exports = hello; 

注意,告訴你的shell如何執行的第一行代碼。

您可以hello.js執行此命令:

chmod u+x hello.js 

現在你可以調用它是這樣的:

./hello.js joe 

它會輸出: 「你好喬」。

您也可以使用相同的文件作爲任何其他JavaScript文件中的Node.js依賴像往常一樣:

const hello = require('./hello'); 
hello('joe'); 
相關問題