I have a JS file (
file.js) that I want to have executed as a command line shell script through nodejs (iojs actually); I'm using MINGW Git Bash on Windows.
The standard way to do it is to put the following shebang in the JS file:
However, I want to pass a cmd line flag to node to activate V8 harmony features, i.e. I want an equivalent of
node --harmony_arrow_functions file.js
when run just as
without the need to pass the flag manually.
#!/usr/bin/env node --harmony_arrow_functions
does not work.
$ ls file.js nodeharmony.sh $ cat nodeharmony.sh #!/bin/sh exec node --harmony_arrow_functions "$@" $ cat file.js #!/bin/sh nodeharmony.sh # or ./nodeharmony.sh, doesn't matter console.log("Hello world") $ ./file.js # this works fine Hello world
But the problem is that when I execute it from another folder, the shell tries to find
nodeharmony.sh in the current working directory, not the directory of
$ cd .. $ ./subfolder/file.js /bin/sh: ./nodeharmony.sh: No such file or directory
Is there a way to create a portable shebang in
file.js such that I can run
file.js from whatever folder, without resorting to having my custom interpreter (
nodeharmony.sh) available in the
#! line, i.e. when I change
file.js to be
#!/bin/sh exec $(dirname $0)/nodeharmony.sh "$0" console.log("Hello world")
then the shell properly passes the dir name and argument but it fails on Node side since the file is not a valid JS code:
$ ./subfolder/file.js d:\CODE\subfolder\file.js:2 exec $(dirname $0)/nodeharmony.sh $0 ^ SyntaxError: Unexpected identifier at exports.runInThisContext (vm.js:54:16) ...
I'd also like to keep the possibility to run my script through
as well as
node --harmony_arrow_functions ./file.js
So the suggested hack to
sed the file contents, delete header and pipe it to node in the shebang is not good in this context, since the latter execution will be impossible.