首页 > 解决方案 > 如何确定当前操作系统是否为带有 Node.js 的 Window 7

问题描述

我已经查看了How do I determine the current operating system with Node.js和类似的帖子,但还没有找到解决方案。

我在 Windows 7 上使用 Docker。这种情况是独一无二的,因为在我的设置下,我必须在配置 webpack 开发服务器代理请求时使用我的本地 IP。这与项目中使用 Windows 10 或 Mac 的其他开发人员不同,他们能够代理 localhost。

这个答案很接近,但有条件地检查 isWindows 还不够好。我需要检查isWindows7。我已经尝试过这些,但我没有看到任何东西(AFAIK)足以证明我正在使用 Windows 7:

const os = require('os');
console.log(os.platform());
console.log(os.type());
console.log(os.release());
console.log(os.arch());
console.log(os.hostname());

标签: node.jswindowswindows-7

解决方案


鉴于:

  1. 我项目中的所有开发人员都使用 Windows 10 或 Windows 7。
  2. 我项目中的所有 Windows 开发人员都使用 Git Bash,所以我不能相信其他不错的 require('child_process').execSync('ver').toString().trim().

那么对于我的用例来说,这就足够了:

const isWin7 = os.release().slice(0, 3) === '6.1';

这个答案所示,从 6.1 开始的版本在技术上可能指的是 Windows 7 或 Windows Server 2008,但对于我的用例,后一种情况永远不会发生。


推荐阅读