前几天在v2看到的一个问题,总结一下4种方法:

JavaScript
// Added in: v8.9.0
const isCoreModule = mod => require.resolve.paths(mod) === null
const isBuiltinModule = mod => {
try {
return require.resolve(mod) === mod
} catch(e) {}
return false
}

https://nodejs.org/api/modules.html#modules_require_resolve_paths_request
https://nodejs.org/api/modules.html#modules_require_resolve_request_options

JavaScript
const builtin = require('module').builtinModules // Added in: v9.3.0
const builtinMods = Object.keys(process.binding('natives'))
.filter(x => !/^_|^(internal|v8|node-inspect)/.test(x))
console.log(builtinMods.includes('fs'), builtinMods.includes('axios'))

https://nodejs.org/api/modules.html#modules_module_builtinmodules

原文地址:https://uedsky.com/2018-07/is-builtin-module/
获取最佳阅读体验并参与讨论,请访问原文