高级前端
前端工程化
【Q294】optionalDependencies 的使用场景是什么

optionalDependencies 的使用场景是什么

Issue 欢迎在 Gtihub Issue 中回答此问题: Issue 296 (opens in a new tab)

Author 回答者: shfshanyue (opens in a new tab)

当一个包是可依赖可不依赖时,可采用 optionalDependencies,但需要在代码中做好异常处理。

chokidar (opens in a new tab)fsevents 的引入

{
  "optionalDependencies": {
    "fsevents": "~2.1.2"
  }
}
let fsevents;
try {
  fsevents = require("fsevents");
} catch (error) {
  if (process.env.CHOKIDAR_PRINT_FSEVENTS_REQUIRE_ERROR) console.error(error);
}