4 ms·
I really like NodeJS for more complex system-scripting. In contrast with Python: 1) There aren't two disjoint versions to juggle 2) Dependency installation is
by brundolf 6y ago
I really like NodeJS for more complex system-scripting. In contrast with Python:
1) There aren't two disjoint versions to juggle
2) Dependency installation is clean and contained
3) Dependencies can easily be installed locally or globally, depending on your preference for isolation vs reuse
4) Lots of packages are available for doing cross-platform (read: Windows compatible) operations
5) JS makes it really easy to do efficient async tasks, for example high-IO scripting involving reading and writing lots of files and making network requests (without any dependencies, I might add)
At my last company I converted all our shell scripts to Node scripts when the pandemic hit, so they'd work predictably on Windows machines, and it was great. These days I'd do it that way to begin with.
- lordgroff 6y agoJavaScript as a shell replacement, what a time to be alive! I am familiar with and use quite a bit of js on the front end, but how is node for shelling out? This is something I find Python to be quite terrible at.
- brundolf 6y agoI wouldn't use it as an interactive prompt personally - it's a bit too verbose - though I did know someone in college who did that :) Calling shell commands is reasonably straightforward: const { exec} = require('child_process') const child = exec('ls', ['-lh', '/usr']) console.log('error', child.error) console.log('stdout ', child.stdout) console.log('stderr ', child.stderr) However, you can also get cool packages like this to use instead: https://github.com/shelljs/shelljs https://github.com/shelljs/shelljs I will admit though that in most of the cases where I've used it I didn't really do any shelling-out, so I can't speak much to how that experience scales in practice. Mostly I was either 1) doing self-contained logic with files, web requests, etc, or 2) delegating out to JS build tools (which even when they have a CLI, almost always expose a nice JS API too). Ymmv, etc.
- mekster 6y agoIt's far easier to do it on perl. $output = `ls -lh /usr`; I don't think I can remember all the boilerplate of using the wrong language for the job if I only happen to write that once a month or less. Even PHP is easy. $output = shell_exec('ls -lh /usr');
- sbacic 6y agoLet me put it this way; I recently wrote a script to parse the output of last and format it. The Node version was some 90 LoC but if I included the dependencies it came to about 5 MB. Then I rewrote the same script in Python3 and it was 60 LoC and no dependencies that needed to be installed separately. I will admit that the JS code looked _a lot_ cleaner than the the Python code though.
- schwartzworld 6y agoNode is great for shell scripts. Async programming is a breeze in JavaScript. I wrote a quick wrapper function around child_process.exec to promisify it, and this allows me to use features like async / await and Promise.all in my scripts.
- chatmasta 6y agoI tend to write a lot of bash, partially for masochistic reasons, but I would probably choose Node over Python in 2021 for basic dev tooling scripts. Setting up a node environment with nvm can be done in a few lines of code, and it’s pretty reliable across platforms. Python is just too difficult to support consistently. Really you should pick whatever you currently have in your codebase. We happen to have both Python and Node, with the languages generally being developed separately by different devs. So we’ve got tools in both, but a Node developer is unlikely to ever need/use the Python tools (although they can all run in Docker anyway). What annoys me a bit is that pre-commit requires a Python env to bootstrap itself, but it can install nvm without any fuss.