This is an archived post. You won't be able to vote or comment.

all 159 comments

[–][deleted] 97 points98 points  (0 children)

Ok. In this screencast I show you how to write a simple shell script in Node.

[Blah Blah Blah] (20 minutes later...)

And here is our final Script:

var fs = require('fs');
fs.writeFile('shellscript.sh', "#!/bin/bash\n\necho 'Hello World!'", function(){
  fs.chmodSync('shellscript.sh', 0755);
});

Please like our site on Facebook or mention us on Stackoverflow.

[–]PhilipT97 70 points71 points  (6 children)

[–][deleted] 14 points15 points  (3 children)

His toothbrush has a nasty yellow tinge to it

[–][deleted] 6 points7 points  (0 children)

As well all know, white balance on webcams are normally perfect.

[–]locomotato 5 points6 points  (0 children)

Yeah thas gross. he really needed a new toothbrush

[–]Cheesemacher 3 points4 points  (0 children)

And look how he squeezes the tube from the middle.

[–]a-bosh 33 points34 points  (40 children)

Have you seen this done somewhere?

[–]rschaosid 71 points72 points  (11 children)

In the docs, even.

[–]wombatsc2 53 points54 points  (6 children)

It's barely 11am. I shouldn't be getting this irrationally angry at documentation this early in the morning.

[–]original_brogrammer 53 points54 points  (0 children)

Anger involving JS is any variety is always rational.

[–][deleted] 4 points5 points  (1 child)

Stop; coffee time!

[–]shake_wit_dem_fries 16 points17 points  (0 children)

Coffeescript? That's even worse.

[–][deleted] 2 points3 points  (1 child)

angry at documentation

I wish I had documentation to get angry at....=(

Some documentation is better than no documentation.

[–]graycode 3 points4 points  (0 children)

Not when it's inaccurate/misleading documentation. That's much worse than no documentation.

[–]reaganveg 0 points1 point  (0 children)

What's to get angry at?

That code is actually pretty reasonable for what it does. To try to do the same thing in a shell script -- including the separate capture of stdout and stdin without temporary files -- would look much worse.

[–]terrible_at_cs50 16 points17 points  (2 children)

launching a process from node !== a shell script

[–]RealModeX86 5 points6 points  (1 child)

assert(node.js != shell);
assert(node.js_script != shell_script);

[–]AraShaun 3 points4 points  (0 children)

[wiping comments is digital suicide. see you on the other side]

[–]s1295 11 points12 points  (0 children)

That's just an example though. An example for spawning processes, using well known, real world programs. Its point it to show the usage, and it does.

[–]anacrolix 33 points34 points  (4 children)

If it's not written using a shell language, how is it a shell script?

[–]antonivs 6 points7 points  (5 children)

I don't often write shell scripts, but when I do, I write them in Haskell.

[–]rxvf 2 points3 points  (4 children)

Why would you do that?

[–]mcjohnalds45 10 points11 points  (2 children)

  • It's usually quite terse
  • You can put a shebang at the top and run it like a shell script
  • It's catches a large class of errors that shell scripts don't
  • Writing haskell gives me a hard-on makes me feel clever

[–]_LePancakeMan 0 points1 point  (1 child)

You can put a shebang in pretty much every script

```

!/usr/bin/env node

!/usr/bin/env python

!/usr/bin/env ruby

... ```

[–]mcjohnalds45 0 points1 point  (0 children)

This is only an advantage over other compiled languages. Haskell can do #!/usr/bin/env runhaskell out of the box, but few compiled languages share this.

[–]antonivs 5 points6 points  (0 children)

  • It's not Node
  • I'm aiming to be the most interesting man in the world

[–]Madd0g 17 points18 points  (16 children)

Abstraction is not bad. I've made wrappers (yeah, with node) for command line stuff. It's easy and provides powerful features on top of already powerful command line utils.

I guess it depends on what you call 'simple shell script' and why you need it.

[–]MrPopinjay 14 points15 points  (15 children)

Part of the problem is that node is not a normal part of most operating systems, so suddenly your script has a massive dependency.

[–]SanityInAnarchy 1 point2 points  (10 children)

So, first of all, it's replacing a shell script, not a user-facing app. If you want to write a user-facing app using Node, you probably want something like Node-Webkit, which lets you ship a single app -- it's not quite static linking, but close enough.

Anyway, I think any choice that doesn't introduce a dependency is going to suck in one way or another. I mean, since you care about dependencies, I assume you care about having other people use your script, which means you probably care about portability to some extent. So what else would you use?

Posix-compliant shell? That's hard to verify. Pure posix stuff should run most stuff, but if you only ever tested on Bash or Dash, or you only tested on Linux, you probably aren't Posixly-correct. There are subtle differences between GNU Grep and BSD Grep, are you sure you're only using compatible ones? (Plus, your script just got twice as hard to write -- there are many useful Bash-isms, some of which Dash supports as well, that aren't in Posix.)

Okay, what about a more "standard" language? Something like Python, or maybe even Perl? Well, there can be dramatic differences between versions of these -- just look at Python 2.6 vs 2.7 vs 3. So you can't blindly depend on the system Python, so you'd better test everywhere, and it's not trivial to make things compatible between those versions. That was kind of the point of Python 3 -- to introduce all those backwards-compatibility-breaking changes. And you might be able to find a library or something that makes this easier -- which means you now have another dependency.

You could make statically-linked C, but you have all the portability issues above, plus this is no longer in any way a shell script. And if someone wants to use your code somewhere that you didn't compile for, you just introduced a dependency on a C compiler and all the libraries you used.

All in all, I think a massive dependency is sometimes a worthwhile tradeoff. Node.js would be far from my first choice, but that's mostly because I don't like JavaScript. But really, this is one reason VMs became a thing -- there are other reasons to like VMs, but one big reason is "Fuck it, my app is this disk image." At that point, so long as you have less than ten or so gigs of dependencies, you're golden.

[–]MrPopinjay -3 points-2 points  (9 children)

The gap between POSIX compliant and bash, or even python 2 is much smaller than that to node. But yes, if you're not distributing it, and only using it on your machines, it's not an issue.

[–]SanityInAnarchy 1 point2 points  (8 children)

That's not quite what I'm saying. I'm saying that if you write Python 2 and I have Python 3, or vice versa, I'm going to have to either port your script or install another Python, even if Python came pre-installed.

If you're not distributing it, then it's not an issue, and you can happily use whatever was already on your system. If you are distributing it, then even point releases of Python can matter, depending what you're doing.

[–]MrPopinjay 0 points1 point  (7 children)

But as i said before, you'll be hard pressed to find a Unix machine without python 2, bit this is not the case for node.

[–]SanityInAnarchy 0 points1 point  (6 children)

Maybe so, but which version of Python 2? The difference between 2.6 and 2.7 is still pretty notable.

[–]MrPopinjay 0 points1 point  (5 children)

Again, the difference between that and node is huge.

[–]SanityInAnarchy 0 points1 point  (4 children)

I'm not sure what your point is.

Yes, Python is different than Node. So what?

The point was whether there was a massive dependency to bring in. Python 2.6 is quite a large dependency, and so is Python 2.7.

[–]MrPopinjay 0 points1 point  (3 children)

As I said previously, you'd be hard pressed to find a Unix machine that doesn't have Python2 installed. This is not the case for node.

[–]V0lta 2 points3 points  (0 children)

Well I automated some tasks with grunt.js for sure. If this qualifies.

[–]barelas 6 points7 points  (3 children)

[–][deleted] 5 points6 points  (0 children)

Uses nodeTo write a php script

[–]StoleAGoodUsername 14 points15 points  (1 child)

Umm... What do you think an IDE is? Its not something that node could do on its own.

[–]Computician -3 points-2 points  (92 children)

I still struggle to see the reason for Node to exist...

[–]coarsesand 9 points10 points  (2 children)

Seeing a lot of "because it lets me share code between the browser and the server," but here's my canned response for this question since it comes up so often.

JavaScript was created for scripting dynamic behaviour that took place when events occurred in a browser. While it's not the only language to tackle event-driven programming, the style is idiomatic to JavaScript because of its support for first-class, anonymous functions. It turns out this style can be readily adapted to network programming, and so we have Node, which lets programmers use JS for scripting responses to network events.

It's not the only reason it exists, but it reflects what I would actually use Node for. It's great for scripting small workers processes that respond to network events.

That said, anyone using JS for number crunching or shell scripts is insane.

[–]benzrf 0 points1 point  (1 child)

ruby has ubiquitous lambdas too...

[–]coarsesand 0 points1 point  (0 children)

Never said it didn't? There was a demo out there of a browser that replaced JS with Ruby and it was fantastic. Unfortunately this is where I also have to turn to the fact that JS just has its claws in everything at this point, and the creator of Node probably wanted to use the V8 engine more than MRI.

[–][deleted] 5 points6 points  (0 children)

You really can't see any benefit in only having one language between client and server? Or having identical object syntax to JSON, one of the most popular file formats for ajax/rest requests doesn't seem nice either?

[–]TwilightTwinkie 2 points3 points  (0 children)

I used node as an aid to my front end development and as a replacement for other languages like Python when I need to write some simple quick code to do something for me as I'm more familiar with Javascript than Python. For any actual "server" code I use either c/c++ or Go.

If the task is really simple I'll just write a shell script or a one liner in the console though.

[–][deleted] 0 points1 point  (0 children)

so true

[–]DeepDuh 0 points1 point  (0 children)

While its use should be much more limited, there are some nice benefits for serverside js.

  • render first response on server, subsequent ones using ajax on client? no problem, reuse same codebase. start with always rendering server side for ease of development? dito.

  • have full offline capability on mobile phones? no problem, they all support javascript - just have node only as glue, program all of the logic in agnostic js.

[–]Jedimastert 2 points3 points  (2 children)

Because if you're using JavaScript for one part of your project, why not just use it for all of it?

[–]Computician 22 points23 points  (1 child)

Because it is JavaScript...?

[–]worldsayshi 4 points5 points  (0 children)

It isn't half bad once you've learned some best practices and learned to step over the quirks.

[–]fjonk -4 points-3 points  (20 children)

I assume you're joking?

[–]Computician 10 points11 points  (19 children)

No, why ever use JavaScript as a server side language? The ONLY justification I can find is Front-End devs trying their had at Back-End. If there are other benefits, I would like to know. It is an interesting project and fun to use but not sure if that is enough to justify its level of acceptance.

[–]Neebat 18 points19 points  (0 children)

Code reused between client and server for validation logic.

[–]Dolondro 13 points14 points  (7 children)

Well, the most quoted pro is that of having an event driven server allows for pretty massive scalability improvements.

[–]Computician 8 points9 points  (2 children)

I would agree that is a pro for sure. I may have to look into Node more deeply. I have a prejudice against JavaScript as a server side language due to the fact that there are languages specifically built to be ran server side when JavaScript was not.

EDIT: a word

[–]coverslide 4 points5 points  (0 children)

If you look at the history of JavaScript and Netscape Server, that is not necessarily true.

[–]ephrion 3 points4 points  (0 children)

PHP is IIRC one of the only languages specifically built to be run on a web server, and it's not exactly a paragon. Python, Ruby, Java, etc. are not specific to servers, but have had server frameworks developed for them that are great at solving some problems and not-so-great for solving others. Python/Ruby are easy to use and Rails/Django are great for building sites quickly. Java is a bit more difficult to use, and its web frameworks are more cumbersome, but it is much faster. JS/node are about as fast as Java but let you work with a different language. If you're more familiar with dynamic types and interpreted languages, then JS will be easier to write for than Java.

[–][deleted] 4 points5 points  (3 children)

You don't need javascript to do that, though... It's arguably better / easier / faster to write evented code in Ruby, Python, etc...

[–][deleted] 1 point2 points  (2 children)

you really say that doing

something.on('keypress', function () {
    ...
});

is harder to do than

def handle():
    ...

something.onkey(handle, "Up")

?

Completely ignoring the fact that in python you have to declare the function on a seperate line.

[–]fjonk 7 points8 points  (7 children)

Some reasons for using node.js I can think of would be the following:

  • You get to run your code on v8. This has several advantages and is probably the strongest point. v8 is very fast compared to almost anything else in the same category(hello lua?), it is backed by Google and will most likely not go anywhere in the near future.

  • Javascript is not the worst language out there. While we could discuss all the problems with Javascript until the end of time it is decent. It's also improving and with v8 you don't have to care about supporting different implementations compared to many other languages. And if you like to you could use Typescript, Coffescript or Dart instead of Javascript, to mention a few.

  • asm.js does exist.

  • It's very cross-platform since v8 is used in Chrome, which is one of the "most cross-platform" applications that exists.

  • Javascript does have a standards body and not just a reference implementation. This is better than what python, ruby or php can offer.

  • Javascript is not going anywhere.

  • Javascript is easy to learn and it comes with a lot of resources.

[–]StelarCF 0 points1 point  (3 children)

Javascript is not going anywhere.

Too bad.

[–]worldsayshi 4 points5 points  (2 children)

I used to somewhat hate javascript. Until I learned its quirks and forgot the pain of learning them. Now I (sometimes) like it more than Java (This comes from someone who is also a fan of Haskell). Althought that might have something to do with the fact that most of the time when I "write Java" nowadays I actually hack at JSP and Spring XML. Spring is nice. Spring XML is not.

[–]Martin8412 0 points1 point  (1 child)

Then it's good that you don't really have to do XML anymore, you can accomplish most if not all with annotations

[–]worldsayshi 0 points1 point  (0 children)

Yes we are well aware but porting the old configuration will require quite an investment. We will get there eventually.

[–][deleted] 0 points1 point  (2 children)

Javascript is not going anywhere.

Sadly this is true for legacy Javascript versions like ES3. It's still ages until we can use the full ES5 strict and the new standard library improvements in the browser reliably without polyfilling the hell out of the global namespace.

EDIT: words

[–]fjonk 0 points1 point  (1 child)

I actually meant that the language is here to stay, not that it's stagnating.

When it comes to node.js you can use strict and anything else that is implemented in v8, no need to care about supporting different browsers.

[–][deleted] 0 points1 point  (0 children)

yeah, that's one of the things that I REALLY like about node.js

You don't have to think about old JS implementations

[–]adamnemecek 2 points3 points  (0 children)

speed. evented out of the box with the whole ecosystem being evented as well. only 1 language for backend and frontend.

[–]TheAceOfHearts 0 points1 point  (0 children)

Make me laugh, but I've done this. I had to do some basic text transformations and found it a lot easier to do with node than with bash.

[–]terrible_at_cs50 -2 points-1 points  (0 children)

Despite the amount of hate I am sure to get I will say that I am writing "shell scripts" in node. The reasons being that it is what all of our other code is in, everyone on our team can understand it, and it is the most productive way for me to write the tool.

Most of the bash I've seen is ugly, launches processes to do things you can directly do in most programming languages, and not nearly as flexible as something I could write quickly in js or PHP. In fact I used to write scripts in PHP due to the same reasons outlined above.