News for hackers. Stuff that matters.
Re:How to write a command line application in Node.js by qrohlf on Friday August 09, 2013 @06:54PM (#6189117)
Is there a reason to build a command line application in Node.js? It doesn't really seem like Node is the right tool for the job...
Re:How to write a command line application in Node.js by rgbrgb on Friday August 09, 2013 @07:05PM (#6189156)
It's fun, simple, fast, and has a good package manager. JS is a high level scripting language like python or ruby but gives significant speed benefits in a lot of cases (in particular, the startup time of CLI programs written in Ruby always bothers me).

If you want to write in C for performance reasons, go ahead. But if your other alternatives are Python or Ruby and you already know JS, give node a go -- it's pretty fun.

Re:How to write a command line application in Node.js by retr0h on Friday August 09, 2013 @11:17PM (#6189946)
This is what happens when you let front-end developers loose in your infrastructure.
Re:How to write a command line application in Node.js by gcr on Friday August 09, 2013 @11:45PM (#6189988)
Don't knock it 'till you try it. Most developers would balk at the idea of creating a project in something that isn't their favorite language, especially when they've been burned by poor uses of that language before.
Re:How to write a command line application in Node.js by retr0h on Saturday August 10, 2013 @12:16AM (#6190025)
I have written plenty of JS, and absolutely hate it.
Re:How to write a command line application in Node.js by akbar501 on Saturday August 10, 2013 @02:14AM (#6190240)
2 plus years ago there may have been merit to your implicit assumption that JavaScript is limited to front-end developers.

However, today there is more diversity in the JavaScript developer community. For example, Yehuda Katz is both a server-side (Rails) team member and client-side (JQuery, Ember) developer. Also, system engineers have begun working in JS. The Meteor team is a good example of system engineers working on JS.

Re:How to write a command line application in Node.js by axaxs on Saturday August 10, 2013 @12:57AM (#6190109)
Well, there's my daily laugh :)
Re:How to write a command line application in Node.js by aegiso on Friday August 09, 2013 @07:06PM (#6189160)
If you're doing crazy heavy processing or something, it's not the right tool. But you wouldn't do that in JS. You'd shell out or call something native.

Node is best used managing parallel streams of stuff. Perhaps surprisingly, a command line application is a great example of that.

I would much rather build a nontrivial command line app in node than in anything else. Try it, you'll see.

Re:How to write a command line application in Node.js by 10098 on Friday August 09, 2013 @07:16PM (#6189206)
There are tons of better options to choose from. If you're not making a web UI, literally any choice is better than Javascript, unless that is the only language you know.
Re:How to write a command line application in Node.js by bcoates on Friday August 09, 2013 @07:36PM (#6189287)
Compared to most languages you'd use for a commandline application, node has a simple, usable async I/O interface, and the libraries you can get for it actually assume async.

If you need that, there are only a handful of choices and node is one of them.

Re:How to write a command line application in Node.js by mhd on Friday August 09, 2013 @11:19PM (#6189951)
Which scripting language doesn't have an async library available (most of which don't descend into the Chinese Hell of Being Called Back Ad Infinitum)? Granted, most of the other languages have lots of libraries that don't play well with async, but then again, that's mostly because they have lots of libraries. And it's not like there's a guarantee that a random Node package won't screw you up, either.

I'm not really arguing against using JavaScript for command-line interfaces. I'd much rather see that than the same in PHP, and if the programmers don't know Perl, Python or Ruby, then why not? We're talking about an area where there are still way to many hacks that consist of badly written bash scripts with lots of inline sed and awk...

Re:How to write a command line application in Node.js by zeckalpha on Sunday August 11, 2013 @01:31AM (#6193969)
concurrency != async
Re:How to write a command line application in Node.js by aegiso on Friday August 09, 2013 @08:58PM (#6189593)
Brainfuck, then :).

People don't use node for the Javascript. They use it for the modules, and node is the glue that coheres them (and does an admirable job at that).

When you're comparing writing a CLI app in node to writing it in something else, it's a world of difference, and that difference is npm. With good modules, writing a CLI app has little to do with Javascript -- you're more specifying config and flow by passing some options and streams around, and you're done.

Re:How to write a command line application in Node.js by 10098 on Saturday August 10, 2013 @02:11AM (#6190232)
How about writing it in Perl and using CPAN instead of npm then? There are tons of modules in CPAN. But since Perl is not cool any more, there's always Python and pip.
Re:How to write a command line application in Node.js by kephra on Saturday August 10, 2013 @05:28AM (#6190533)
I have yet to see any language repository that constrains quality in the way CPAN does with CPAN testers.

CPAN modules must provide POD documentation, must provide a regression test, and must run on several dozen platforms before PAUSE accepts to publish it on CPAN.

Python or Ruby modules, or my favorite Lua, are a bunch of junk compared to CPAN.

Re:How to write a command line application in Node.js by akbar501 on Friday August 09, 2013 @07:11PM (#6189179)
Yes.

If a person already knows JavaScript well, then it will be easier for them to write a CLI app/utility/script in JS than it would be to write a shell script.

Another reason to write in JS would be to gain code reuse. If the person has a web app that uses node, then they can share code between the web and CLI applications.

Re:How to write a command line application in Node.js by stock_toaster on Friday August 09, 2013 @07:51PM (#6189338)

  > Another reason to write in JS would be to gain code reuse.
  > If the person has a web app that uses node, then they can
  > share code between the web and CLI applications.
I often see this argument presented for just about any server side js. I often wonder about actual real world reuse though.
Re:How to write a command line application in Node.js by akbar501 on Saturday August 10, 2013 @01:58AM (#6190215)
I actually reuse the same code a lot, which is a very nice feature once you get used to it.

I share code between the client and server. For example, I have data type checks where I reuse the same library and the same application specific code on both the client and server.

I expose substantial portions of my server-side code via the command line. I also frequently call shell scripts from node as there are certain things I find easier to write in a shell script.

That said, I don't know if this amount of reuse is typical. And, code reuse is clearly not the only consideration.

Re:How to write a command line application in Node.js by daleharvey on Friday August 09, 2013 @09:16PM (#6189648)
Look at the top 2 depended on modules modules in the entire of the server side node.js ecosystem and guess what they were written for - https://npmjs.org/

I wrote a database specifically for web browsers and someone case along and write a relatively small pull request and it now works in node / on the server / with a http api - http://pouchdb.com

Re:How to write a command line application in Node.js by chrsstrm on Friday August 09, 2013 @07:49PM (#6189327)
Depends on the job. I'm writing a command line app in Node right now to automate a couple thousand SQL queries for a db I'm working with. Node wasn't my first choice, but I looked at how to connect both Ruby and Python to MySQL and it seemed like more trouble than it was worth for a one-time use utility. Node was just 'npm install mysql'. Might not be the best way, but I only need to use it once.
Re:How to write a command line application in Node.js by FooBarWidget on Friday August 09, 2013 @08:00PM (#6189372)
Isn't Ruby just 'gem install mysql'?
Re:How to write a command line application in Node.js by chrsstrm on Friday August 09, 2013 @08:18PM (#6189456)
I don't recall the instructions I looked at, but I know what I read was much more than a gem install. Correct me if I'm wrong, I actually wanted to use Ruby first.
Re:How to write a command line application in Node.js by jrumbut on Saturday August 10, 2013 @06:08AM (#6190582)
Sorry if this is off topic, but I remember having this exact issue when I was learning Ruby, maybe I can provide some clarity.

For raw access to your MySQL database, the gem 'mysql2' is preferred (github with a simple tutorial: https://github.com/brianmario/mysql2).

mysql2 seems to have taken it's interface design from Perl's DBI, which is cool if you're used to that but might feel a little foreign.

If you'd like something that gives you a little more help, or there's substantial business logic in your script, take a look at ActiveRecord. This StackOverflow question (http://stackoverflow.com/questions/16683903/sinatra-mysql-an...) has a nice example of how you might set up your script to use AR.

My email's in my profile if you'd like any future help.

Re:How to write a command line application in Node.js by chrsstrm on Sunday August 11, 2013 @04:43AM (#6194358)
Thanks. Appreciate the info.
Re:How to write a command line application in Node.js by kephra on Saturday August 10, 2013 @05:23AM (#6190521)
What is the memory footprint of node? What is the startup cost?

imho, node as command line makes even less sense then Smalltalk, Java or Mono. Those are heavy weight languages that have no place in interactive commandline scripting.

Better choices are the classical languages like Perl or Python, and Lua or awk has even less startup costs.

Re:How to write a command line application in Node.js by jdlshore on Saturday August 10, 2013 @02:05PM (#6192025)
> What is the memory footprint of node? What is the startup cost?

It's fast enough, and low-overhead enough, that I don't notice any overhead when running my Node.js build scripts. And really, that's all that matters.

Out of curiosity, I wrote a little script to test it:

    #!/usr/local/bin/node
    console.log(process.memoryUsage());
And here's the output (memory usage is in bytes):

    jshore$ time ./deleteme.js 
    { rss: 12660736, heapTotal: 4083456, heapUsed: 2131864 }

    real   0m0.055s
    user   0m0.043s
    sys    0m0.011s
Re:How to write a command line application in Node.js by DrJokepu on Friday August 09, 2013 @08:48PM (#6189566)
I don't think it has anything to do with the interface. Is your program mainly about handling input and output, tied together with some business logic? If yes, Node.js is probably a good choice. If not, Node.js might not be a very good choice.
Re:How to write a command line application in Node.js by derengel on Saturday August 10, 2013 @12:30AM (#6190064)
I was thinking the same, does nodejs even has a complete linux api interface like python or ruby?

EDIT: Ok, just went through the nodejs api, they have cover a lot of ground now :)