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?
type() Method. The os. type() method is an inbuilt application programming interface of the os module which is used to get Operating system name.
Parallelism, on the other hand, is achieved through the use of the Web Workers API. Web Workers are probably the only way to achieve “true” multi-processing in JavaScript. We say “true” here because with setInterval() , setTimeOut() , XMLHttpRequest , async/await , and event handlers, we can mimic parallelism.
There is now an os
module: Node OS Module Docs. It has a function for getting the platform os.platform
The docs don't give return values. So, I've documented some below. Results are for Ubuntu 12.04 64-bit, OS X 10.8.5, Windows 7 64-bit and a Joyent SmartMachine, respectively. Tests conducted on Node 0.10.
os.type()
: 'Linux'
os.platform()
: 'linux'
os.arch()
: 'x64'
os.type()
: 'Darwin'
os.platform()
: 'darwin'
os.arch()
: 'x64'
os.type()
: 'Windows_NT'
os.platform()
: 'win32'
os.arch()
: 'x64'
os.type()
: 'SunOS'
os.platform()
: 'sunos'
os.arch()
: 'ia32'
Note: on Windows 7 64-bit node may incorrectly report os.arch()
as ia32
. This is a documented bug: os.arch should be the architecture of the OS not of the process
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With