2

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.

ptf
  • 3,210
  • 8
  • 34
  • 67

1 Answers1

1

It works in Linux Mint 15 after removing the shebang line and invoking it through:

$ node code.js emails emailsOut

Also adding the OS-specific line #!/usr/bin/node and chmod +x code.js I can run it through

$ ./code.js emails emailsOut

Windows unfortunately doesn't work with shebang lines as such. A workaround can be found here.

Community
  • 1
  • 1
SomeKittens
  • 38,868
  • 19
  • 114
  • 143
  • I forgot to mention that it works if I run it with the `node` command (without the shebang). I need the shebang for it to work as an command line app. Tried using `chmod +x`, but no difference. I'm on windows. – ptf Jan 18 '14 at 19:31
  • That made it work in the native windows command line. But, unfortunately, it doesn't work with the Git Bash, as it looks for the interpreter. I did run a much simpler command line app in the Git Bash, with the shebang, and it worked. – ptf Jan 18 '14 at 20:54