30

I like javascript, so I was excited when I heard about Node.js, a V8-based Javascript runtime. I would prefer to do my shell scripting going forward in Javascript. My issue is this: how can I run my scripts without calling node ~/Scripts/myscript.js? After I chmod +x my script, it tries to run as a bash script instead of a Node.js javascript.

skaffman
  • 381,978
  • 94
  • 789
  • 754
Just Jake
  • 4,595
  • 3
  • 23
  • 27
  • imho this belongs to stackoverflow.com – akira Jan 25 '11 at 07:21
  • 1
    @akira I posted it here because it concernes tool use rather than programming topics. My questions are about how an operating system handles script files, not about how to program said scripts. – Just Jake Jan 25 '11 at 17:38
  • It's actually quite easy to do this in Geany: http://stackoverflow.com/questions/12464679/run-a-node-js-server-from-geany/12465826#12465826 – Anderson Green Sep 17 '12 at 19:42

2 Answers2

38

Whats making your current shell starting the bash is that your current shell (bash?) has no clue about what to do with a file.js. Thats why the gods of unix invented the shebang for:

The character sequence consisting of the characters number sign and exclamation point (#!), when it occurs as the first two characters in the first line of a text file. In this case, the program loader in Unix-like operating systems parses the rest of the first line as an interpreter directive and invokes the program specified after the character sequence with any command line options specified as parameters.

So, in your case I would try to put

 #!/usr/bin/env node

at the top of the script. You can see that beeing applied for example in the 'inode' (interactive node.js) shell, which might be another option to fire your scripts.

https://github.com/bancek/node-interactive-shell/blob/master/inode.js

peterh
  • 9,698
  • 15
  • 68
  • 87
akira
  • 5,714
  • 26
  • 36
  • 24
    Actually he should use `#!/usr/bin/env node`. PS: Having Node installed as root is... as bad idea, always install it in `~/.local`, one should also install npm there. Otherwise one has to sudo npm for installing stuff, and you node packages can have post-install scripts ;) – Ivo Wetzel Jan 27 '11 at 07:20
  • @Ivo Wetzel how would I go about moving my Node.js install to ~/.local? Should I just uninstall and reinstall? I build from source using 'make' – Just Jake Jan 29 '11 at 19:22
  • Uninstall the current one and then refer to this gist: https://gist.github.com/579814#file_xgd_freedesktop.org_style.sh – Ivo Wetzel Jan 29 '11 at 20:36
  • How can you keep the terminal window open even after the script finishes running? I've noticed that the terminal window usually opens and closes quickly instead of staying open when I try to run the script in the terminal. – Anderson Green Sep 17 '12 at 16:19
  • @AndersonGreen open a new window then `cd` to the directory of the script that you want to run, then type `./YourScriptName` to run it. – Just Jake Sep 20 '12 at 23:07
  • @JustJake Is it possible to do the same thing using a shell script (instead of typing these commands manually?) – Anderson Green Sep 21 '12 at 03:01
  • @AndersonGreen: write the steps down in a script. – akira Sep 21 '12 at 05:40
  • It works but I wonder how it can work, cause # character doesn't make a comment in javascript. A syntax error is thrown if I put # character in any place but the beginning of the file. Does node throws the shebang line before passing the program to the javascript engine? – Yaroslav Aug 22 '13 at 12:19
  • it's real magic what a gifted programmer can do. eg, look at the first 2 chars of a file and check, if there is a `#!`. – akira Aug 22 '13 at 14:02
  • @Yaroslav - Node is smart enough to discard the first line if it starts with #!, so the JavaScript engine never sees it. – Daniel Earwicker Feb 03 '14 at 16:38
  • I suggest to start to use capital letters. – peterh Mar 10 '17 at 20:10
  • Not only Node.js is smart to discard the shebang line before handing your script to the interpreter, great IDEs like Webstorm are able to do the same :-) – Lucio Paiva Jun 08 '17 at 17:53
1

You can always simply create a shell script that runs node for you.

Alternatively, if you want to create a script that can run in an environment that doesn't have node.js installed, you can use installer-maker.

B T
  • 46,771
  • 31
  • 164
  • 191