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?

4
  • 5
    OK, so after asking around at the office, here's the answer that basically works (it pops up CYGWIN for Windows, but hey) var os = require("os"); os.type(); Jul 1, 2011 at 17:16
  • I assume you've tried nodejs.org/docs/v0.4.9/api/fs.html?
    – hvgotcodes
    Jul 1, 2011 at 17:17
  • 1
    fs doesn't get the job done; os will. Should have guessed, but am new to node. Jul 1, 2011 at 17:17
  • nodejs.org/api/os.html
    – user405398
    Mar 12, 2013 at 7:25

4 Answers 4

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

4
  • On Mac, os.platform() is darwin. Not sure of the type/arch, since most modern day macs are universal, or both x86/x64. Jul 15, 2014 at 4:38
  • 1
    @Qix Thanks! Luckily I happen to be sitting at a Mac today, so I can add the rest, too! :D Jul 16, 2014 at 19:46
  • Thanks for providing the examples, makes writing my switch statement much easier! May 17, 2018 at 9:39
  • Note that os.platform() is equivalent to process.platform. Mar 13, 2019 at 15:00
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.

2
  • 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. Jul 27, 2011 at 2:13
  • This works, so I'll confirm it. To anyone referencing this question, please also be aware of os.type(). Jul 27, 2011 at 4: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

-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.

Not the answer you're looking for? Browse other questions tagged or ask your own question.