javascript - 为需要的 Node 模块创建工厂

标签 javascript node.js linux module require

我想根据所需的操作系统 windows/linux 创建工厂/或类似的方法 例如,如果 linux 使用

var isLinux = /^linux/.test(process.platform);
var isWin = /^win/.test(process.platform);

if(isLinux){
var spawn = require('child-process');
}{
elseif(isWin)

var spawn = require('cross-spawn')
}
module.export = spawn;

我知道 cross-spawn 也适用于 windows... 我的问题是有更好的方法将它写在 Node (ver4.4)中而不是 else if

最佳答案

switch(process.platform){
  case 'windows': 
    // code
    break;
  case 'linux':
    // code
    break;
  default:
    return new Error('give us a real OS pls')
}

process.platform === 'windows' ? 
  //code windows : 
  //code linux

这里还有一些值得深思的地方。 Alternatives切换语句。

关于javascript - 为需要的 Node 模块创建工厂,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39104712/

相关文章:

javascript - 观察 AngularJS 服务中的数组变化

javascript - 使用 AngularJS 的默认过滤器和用户定义过滤器的输入字段

node.js - Google Apps 脚本出现错误 : Script Error in Node. js

javascript - Later.js 按计划执行功能?

mysql - 使用 Pentaho 数据集成的 Amazon Redshift 到 Mysql

javascript - 具有图像和边框的跨浏览器兼容的六边形形状(没有 SVG 或背景图像)

javascript - HTML 和 CSS 的误解

node.js - 通过 Node 请求包递归使用 async/await

c - 在 ';' void createCell(struct cell myCell, int type, int immutable) 之前,我一直期望 '('、标识符或 'void'

linux - bc 忽略比例选项