Sunday, May 19, 2024
 Popular · Latest · Hot · Upcoming
194
rated 0 times [  198] [ 4]  / answers: 1 / hits: 19766  / 13 Years ago, fri, july 1, 2011, 12:00:00

How can I access my OS from the node shell?



Context: I'm writing a script in node that I want to open a file with the default program, and the commands for doing this vary across OS.



I've tried standard javascript ways of getting the OS, but they haven't worked (for obvious reasons, there is no navigator in node).



Is it possible to do this without installing nonstandard modules?


More From » node.js

 Answers
3

warning: this might be outdated



there is no navigator object in node.js, because it does not run in the browser. it runs in the system. equivalent to navigator is process. this object holds many information, e.g.



process.platform // linux


if you want to run a web browser, you have to execute it..



var sys = require('sys')
// open google in default browser
// (at least in ubuntu-like systems)
sys.exec('x-www-browser google.com')


this might not work in newer node.js versions (i have 2.x), you might have to use



var child_process = require('child_process')
child_process.exec('x-www-browser google.com')


i guess there is no simple way how to multiplatform run any file with its default app, you would have to find out how to do it in each OS / desktop environment, and do it after OS detection.


[#91395] Thursday, June 30, 2011, 13 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
eanskylerg

Total Points: 524
Total Questions: 107
Total Answers: 100

Location: Colombia
Member since Mon, May 2, 2022
2 Years ago
;