21

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?

MPelletier
  • 16,256
  • 15
  • 86
  • 137
Alex Churchill
  • 4,887
  • 5
  • 30
  • 42

4 Answers4

58

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

Waylon Flinn
  • 19,969
  • 15
  • 70
  • 72
17

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.

mykhal
  • 19,175
  • 11
  • 72
  • 80
  • `process.platform` will return something slightly different from `os.type()` (see my comment above); it gives a bit less granularity, but might be more consistent. There is no cross-platform default extension detection built into node yet, so I'm currently using `child_process.exec` to send different commands to different OS. – Alex Churchill Jul 27 '11 at 02:13
  • This works, so I'll confirm it. To anyone referencing this question, please also be aware of `os.type()`. – Alex Churchill Jul 27 '11 at 04:36
5

console.log('This platform is ' + process.platform);

You can find the documentation at http://nodejs.org/docs/v0.4.10/api/process.html#process.platform

I tested this on Mac OS X with node v0.4.10

mcotton
  • 844
  • 6
  • 11
-1

just use os.platform as mentioned

https://nodejs.org/api/os.html#os_os_platform

os.platform()

Returns the operating system platform. Possible values are 'darwin', 'freebsd', 'linux', 'sunos' or 'win32'. Returns the value of process.platform.

Community
  • 1
  • 1
Japskua
  • 9
  • 2