Is it possible to run Node.js scripts without invoking `node`?
Asked Answered
T

2

32

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.

Tetragonal answered 25/1, 2011 at 5:20 Comment(3)
imho this belongs to stackoverflow.comParturition
@Parturition 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.Tetragonal
It's actually quite easy to do this in Geany: #12465179Velasquez
P
39

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

Parturition answered 25/1, 2011 at 18:8 Comment(12)
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 ;)Karinekariotta
@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'Tetragonal
Uninstall the current one and then refer to this gist: gist.github.com/579814#file_xgd_freedesktop.org_style.shKarinekariotta
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.Velasquez
@AndersonGreen open a new window then cd to the directory of the script that you want to run, then type ./YourScriptName to run it.Tetragonal
@JustJake Is it possible to do the same thing using a shell script (instead of typing these commands manually?)Velasquez
@AndersonGreen: write the steps down in a script.Parturition
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?Divorcement
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 #!.Parturition
@Divorcement - Node is smart enough to discard the first line if it starts with #!, so the JavaScript engine never sees it.Spindly
I suggest to start to use capital letters.Saunders
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 :-)Proviso
P
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.

Portentous answered 29/8, 2014 at 20:31 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.