Live data from Hacker News

How to write a command line application in Node.js

blog.liangzan.net

41–50 of 56 posts

Re: How to write a command line application in Node.js

#41

in this day and age, if i had to create a command line app, from zero, i would definetly start with go.. it has all the functionality in standard libs and the package manager can handle github repos for third-parties (like mongo or mysql drivers).. you just compile it and go! can be as simple than that? the right tool for the job.. if i would create a library that everyone could use and embed i would use C for that..…

In this day and age, drumming off single-sentence dismissals of major languages does not lend one much credibility.

The diversity of programming languages is due in part to the fact that developers enjoy looking at old problems from new angles. Doing so lets us to make creative re-use of libraries and practices that were invented to solve disparate problems, and good things invariably result.

Node.js is a staggering example, but every language you mentioned has its strengths.

Re: How to write a command line application in Node.js

#42

in this day and age, if i had to create a command line app, from zero, i would definetly start with go.. it has all the functionality in standard libs and the package manager can handle github repos for third-parties (like mongo or mysql drivers).. you just compile it and go! can be as simple than that? the right tool for the job.. if i would create a library that everyone could use and embed i would use C for that..…

> the package manager can handle github repos

A package manager with no concept of package versioning, unlike every other major languages' managers.

This is a complete deal breaker for some / many (choose as appropriate).

The idea of "Just track original master or make your own fork if you want a specific version" is insane when all the others seem to be able to do dependency resolution.

> ruby is pretty, eyecandy, bu is like a virgen that cant be touched

Which, even compared to the dismissals of the other languages, makes no sense.

Re: How to write a command line application in Node.js

#43
post #5

Earlier quoted context omitted.

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.

> 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.

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

#44
post #22
post #6

Earlier quoted context omitted.

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.

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 p…

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

#45
post #31
post #3

Earlier quoted context omitted.

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…

This is what happens when you let front-end developers loose in your infrastructure.

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

#46

The number one reason to write CLI apps in node is npm. npm gets package management right, preferring local dependencies to global ones - this means no worrying about what version of a library a user has in their global environment. You also get to bring node's parallel io-centric patterns to your scripting. Need to download a bunch of files from a remote host, process them, and write them to disk? Go for it. But tak…

Need to download a bunch of files from a remote host, process them, and write them to disk?

    cat urls.txt | while read -r url; do 
      base="$(basename "$url")"
      wget "$url" -O - -o "$base.log" | process > "$base" &
    done
    wait
For ad-hoc scripting, I tend to prefer bash, with anything remotely corresponding to heavy lifting assigned to the language with the best library / performance / whatever critical factor for it. Fork-join parallelism in bash is very easy.

Re: How to write a command line application in Node.js

#49
post #2

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...

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

#50
post #44
post #22

Earlier quoted context omitted.

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 p…

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.

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.

Post reply on HN