javascript 从 node.js shell 获取我的操作系统

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6551006/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 21:03:37  来源:igfitidea点击:

Get my OS from the node.js shell

javascriptnode.jsoperating-system

提问by Alex Churchill

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 navigatorin node).

我已经尝试了获取操作系统的标准 javascript 方法,但它们没有奏效(出于显而易见的原因,navigator节点中没有)。

Is it possible to do this without installing nonstandard modules?

是否可以在不安装非标准模块的情况下做到这一点?

采纳答案by mykhal

warning: this might be outdated

警告:这可能已经过时

there is no navigatorobject 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.

navigatornode.js 中没有对象,因为它不在浏览器中运行。它在系统中运行。导航器的“等效”是process. 这个对象包含许多信息,例如

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

这可能不适用于较新的 node.js 版本(我有 2.x),您可能必须使用

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.

我想没有简单的方法可以如何多平台“运行”具有“默认应用程序”的任何文件,您必须找出如何在每个操作系统/桌面环境中执行此操作,并在检测到操作系统后执行此操作。

回答by Waylon Flinn

There is now an osmodule: Node OS Module Docs. It has a function for getting the platform os.platform

现在有一个os模块:Node OS Module Docs。它有一个获取平台的功能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.

文档不提供返回值。所以,我在下面记录了一些。结果分别适用于 Ubuntu 12.04 64 位、OS X 10.8.5、Windows 7 64 位和 Joyent SmartMachine。在 Node 0.10 上进行的测试。

Linux

Linux

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

OS X (Mac)

OS X (Mac)

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

Windows

视窗

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

SmartMachine

智能机

  • os.type(): 'SunOS'
  • os.platform(): 'sunos'
  • os.arch(): 'ia32'
  • 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

注意:在 Windows 7 64 位节点上可能会错误地报告os.arch()ia32. 这是一个记录在案的错误:os.arch 应该是操作系统的架构而不是进程的架构

回答by mcotton

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

console.log('这个平台是' + process.platform);

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

您可以在http://nodejs.org/docs/v0.4.10/api/process.html#process.platform找到文档

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

我在 Mac OS X 上用 node v0.4.10 测试了这个

回答by Japskua

just use os.platform as mentioned

如上所述使用 os.platform

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

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.

操作系统平台()

返回操作系统平台。可能的值为“darwin”、“freebsd”、“linux”、“sunos”或“win32”。返回 process.platform 的值。