我有节点文件,其名称位于author.js
导入文件夹下。我如何使用命令运行这个文件: import author
请帮我解决这个问题?
问问题
350 次
1 回答
1
我希望我能正确理解这一点,如果您希望通过终端执行 author.js 文件中的某些命令/函数,那么您正在寻找的是进程对象上的 argv,用于从终端传递值。
因此,例如在您输入的终端中:
node author.js import authorName
在你的文件中,你有一个监听 argv 对象的函数
if(process.argv[2] == 'import'){
/* some function that does stuff with process.argv[3] value (authorName)*/
}
您将能够直接从命令行运行该函数。
有关 process.argv 的更多信息:https ://nodejs.org/docs/latest/api/process.html#process_process_argv
于 2016-09-12T08:04:23.480 回答