I'm trying to create a node command line app. What I am doing is extracting emails from a document full of information.
But when I have run the command npm link
to make the script globally available, I get these errors:
/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 '
My code passed linting, and I can't see any erros. I'm new to node, so it might be something simple, node specific thing that I don't see.
Here is my code:
#!/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);
});
}
EDIT:
It works if I remove the shebang and run it with node myscript.js file.txt file2.txt
. I'm on windows.
EDIT 2: I'm runing this in the Git Bash. Even on windows it did run a simpler script fine with the shebang.