Nodejs命令行应用程序:意外的令牌“其他”

我正在尝试创build一个节点命令行应用程序。 我正在做的是从充满信息的文件中提取电子邮件。

但是当我运行命令npm link使脚本全局可用时,我得到这些错误:

 /c/Users/petfle/AppData/Roaming/npm/email-extract: line 11: syntax error near unexpected token `else' /c/Users/petfle/AppData/Roaming/npm/email-extract: line 11: `else ' 

我的代码通过linting,我看不到任何erros。 我是新来的节点,所以它可能是简单的,节点特定的东西,我没有看到。

这是我的代码:

 #!/c/Program\ Files/nodejs/node var file_stream = require('fs'); var source = process.argv[2]; var output = process.argv[3]; file_stream.readFile(source, 'utf8', function (error, data) { var list_of_emails = extract_emails(data); write_to_file(list_of_emails); }); function extract_emails(data) { return data.match(/([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)/g); } function write_to_file(emails) { file_stream.writeFile(output, emails.join('\n'), 'utf8', function (error) { console.log('Wrote ' + emails.length + ' lines to ' + output); }); } 

编辑:它工作,如果我删除shebang并运行与node myscript.js file.txt file2.txt 。 我在窗户上。

编辑2:我在Git Bash中运行这个。 即使在windows上,它也可以与shebang运行更简单的脚本。

它在Linux Mint 15中工作后,删除shebang行并通过调用它:

 $ node code.js emails emailsOut 

还要添加操作系统特定的行#!/usr/bin/nodechmod +x code.js我可以运行它

 $ ./code.js emails emailsOut 

不幸的是,Windows不能和shebang一样工作。 解决方法可以在这里find。