Running java script from piped output

to run most other scripts through a pipe, something similar to the following is usually enough:

cat script.sh | sh
cat perl.pl | perl -- "<arguments"

However, for javascript command line scripts, i cant seem to get this to work. Any ideas?

cat hull.js

#!/usr/bin/js

console.log("Hello, world!");

attempts to run it by piping:

root@gbrown-VirtualBox:~# cat hull.js | js

[stdin]:1
#!/usr/bin/js
^
SyntaxError: Unexpected token ILLEGAL
    at Object.<anonymous> ([stdin]-wrapper:6:22)
    at Module._compile (module.js:456:26)
    at evalScript (node.js:532:25)
    at Socket.<anonymous> (node.js:154:11)
    at Socket.EventEmitter.emit (events.js:117:20)
    at _stream_readable.js:920:16
    at process._tickCallback (node.js:415:13)
root@gbrown-VirtualBox:~# 
root@gbrown-VirtualBox:~# 
root@gbrown-VirtualBox:~# cat hull.js | node

[stdin]:1
#!/usr/bin/js
^
SyntaxError: Unexpected token ILLEGAL
    at Object.<anonymous> ([stdin]-wrapper:6:22)
    at Module._compile (module.js:456:26)
    at evalScript (node.js:532:25)
    at Socket.<anonymous> (node.js:154:11)
    at Socket.EventEmitter.emit (events.js:117:20)
    at _stream_readable.js:920:16
    at process._tickCallback (node.js:415:13)
root@gbrown-VirtualBox:~# 
root@gbrown-VirtualBox:~# 
root@gbrown-VirtualBox:~# cat hull.js | nodejs

[stdin]:1
#!/usr/bin/js
^
SyntaxError: Unexpected token ILLEGAL
    at Object.<anonymous> ([stdin]-wrapper:6:22)
    at Module._compile (module.js:456:26)
    at evalScript (node.js:532:25)
    at Socket.<anonymous> (node.js:154:11)
    at Socket.EventEmitter.emit (events.js:117:20)
    at _stream_readable.js:920:16
    at process._tickCallback (node.js:415:13)
root@gbrown-VirtualBox:~# 
root@gbrown-VirtualBox:~# 
root@gbrown-VirtualBox:~# cat hull.js | nodejs --

[stdin]:1
#!/usr/bin/js
^
SyntaxError: Unexpected token ILLEGAL
    at Object.<anonymous> ([stdin]-wrapper:6:22)
    at Module._compile (module.js:456:26)
    at evalScript (node.js:532:25)
    at Socket.<anonymous> (node.js:154:11)
    at Socket.EventEmitter.emit (events.js:117:20)
    at _stream_readable.js:920:16
    at process._tickCallback (node.js:415:13)
root@gbrown-VirtualBox:~# 
root@gbrown-VirtualBox:~# cat hull.js | node --

[stdin]:1
#!/usr/bin/js
^
SyntaxError: Unexpected token ILLEGAL
    at Object.<anonymous> ([stdin]-wrapper:6:22)
    at Module._compile (module.js:456:26)
    at evalScript (node.js:532:25)
    at Socket.<anonymous> (node.js:154:11)
    at Socket.EventEmitter.emit (events.js:117:20)
    at _stream_readable.js:920:16
    at process._tickCallback (node.js:415:13)
root@gbrown-VirtualBox:~# 
root@gbrown-VirtualBox:~# 
root@gbrown-VirtualBox:~# cat hull.js | js -
Error: unrecognized flag -
Try --help for options

[stdin]:1
#!/usr/bin/js
^
SyntaxError: Unexpected token ILLEGAL
    at Object.<anonymous> ([stdin]-wrapper:6:22)
    at Module._compile (module.js:456:26)
    at evalScript (node.js:532:25)
    at Socket.<anonymous> (node.js:154:11)
    at Socket.EventEmitter.emit (events.js:117:20)
    at _stream_readable.js:920:16
    at process._tickCallback (node.js:415:13)
root@gbrown-VirtualBox:~# 

The shebang line does only work with languages that interpret the hash-sign as a comment. Javascript does not, so it thinks, that #!/usr/bin/js is a valid statement. By the way, if you feed your script via pipe into the interpreter, the shebang is useless anyway.

Remove the shebang line and it should work:

$ cat hull.js

console.log("Hello, world!");
$ cat hull.js | node
Hello, world!
1 Like

is there anyway to pass arguments to the node command in this situation?

cat hull.js | node -- one two three four five

I don't think that this is possible. But maybe someone with better NodeJS knowledge has an idea?