我正在编写两个节点shell脚本,以便在平台上开发时使用。我们有Mac和Windows的开发人员。是否有一个变量,我可以检查在节点运行一个.sh文件在一个实例和。bat在另一个?
当前回答
var isWin64 = process.env.hasOwnProperty('ProgramFiles(x86)');
其他回答
你正在寻找Node.js的OS原生模块:
v4: https://nodejs.org/dist/latest-v4.x/docs/api/os.html # os_os_platform
或者v5: https://nodejs.org/dist/latest-v5.x/docs/api/os.html#os_os_platform
os.platform () 返回操作系统平台。可能的值为“darwin”、“freebsd”、“linux”、“sunos”或“win32”。返回process.platform的值。
要使用的变量是process.platform
在Mac上,该变量被设置为darwin。在Windows上,它被设置为win32(甚至在64位上)。
当前可能的值为:
aix 达尔文 freebsd linux openbsd sunos win32 android(根据链接,是实验性的)
我刚刚在我的jakeFile顶部设置了这个:
var isWin = process.platform === "win32";
当你在64位窗口上使用32位节点时(如node-webkit或atom-shell开发人员),处理。平台将回显win32
use
function isOSWin64() {
return process.arch === 'x64' || process.env.hasOwnProperty('PROCESSOR_ARCHITEW6432');
}
(详情请点击此处)
var isWin64 = process.env.hasOwnProperty('ProgramFiles(x86)');
对于Node.js v6(及以上版本),有一个专用的操作系统模块,它提供了许多与操作系统相关的实用程序方法。
在我的Windows 10电脑上,它报告了以下内容:
var os = require('os');
console.log(os.type()); // "Windows_NT"
console.log(os.release()); // "10.0.14393"
console.log(os.platform()); // "win32"
你可以在这里阅读完整的文档:https://nodejs.org/api/os.html#os_os_type
推荐文章
- CALL_AND_RETRY_LAST分配失败-进程内存不足
- 在Ubuntu上安装Node.js
- 使用express.js代理
- Node -使用NODE_MODULE_VERSION 51根据不同的Node.js版本编译
- RabbitMQ / AMQP:单队列,同一消息的多个消费者?
- Node.js同步执行系统命令
- 禁用包的postinstall脚本
- Node.js上的html解析器
- 错误:无法找到模块“webpack”
- 在node.js中使用async / await文件系统
- NodeJS -用NPM安装错误
- 如何为本地安装npm包设置自定义位置?
- 回调函数来处理管道的完成
- Express函数中的“res”和“req”参数是什么?
- node.js TypeError:路径必须是绝对路径或指定根路径到res.sendFile[解析JSON失败]