开发者

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

This question already has answers here: How do I determine the current operating system with Node.js (10 answers) Closed 5 years ago.

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 n开发者_StackOverflow中文版onstandard modules?


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


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.


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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜