Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get my OS from the node.js shell [duplicate]

Tags:

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?

like image 464
Alex Churchill Avatar asked Jul 01 '11 17:07

Alex Churchill


People also ask

What will print the name of operating system in NodeJS?

type() Method. The os. type() method is an inbuilt application programming interface of the os module which is used to get Operating system name.

How do you get parallelism in node JS?

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.


2 Answers

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.

Linux

  • os.type() : 'Linux'
  • os.platform() : 'linux'
  • os.arch() : 'x64'

OS X (Mac)

  • os.type() : 'Darwin'
  • os.platform() : 'darwin'
  • os.arch() : 'x64'

Windows

  • os.type() : 'Windows_NT'
  • os.platform() : 'win32'
  • os.arch() : 'x64'

SmartMachine

  • 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

like image 151
Waylon Flinn Avatar answered Nov 06 '22 00:11

Waylon Flinn


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.

like image 37
mykhal Avatar answered Nov 06 '22 01:11

mykhal