比如引入了
const fs = require('fs')
const express = require('express')
如何判断 fs 和 express 哪个是内建模块,哪个是第三方模块
1
yangg 2018-07-23 14:13:10 +08:00
|
2
Tonni 2018-07-23 14:13:23 +08:00
```
require.resolve('jquery').includes('node_modules') ``` 这样? |
3
yangg 2018-07-23 14:19:00 +08:00
const builtinMods = new Set(Object.keys(process.binding('natives')).filter(x => !/^_|^(internal|v8|node-inspect)/.test(x)))
buildinMods.has('fs') |
4
123s 2018-07-23 14:25:01 +08:00
你是想随便了解一下还是?
|
7
zzhbbdbbd OP @123s 是这样的,我最近在写一份 tslint 的自定义 rule,该 rule 的主要功能就是区分 import 的模块的类型,把模块分类,然后代码格式化
|
9
yangg 2018-07-23 14:45:36 +08:00
@zzhbbdbbd 1 楼那个包的源码,
`process.binding` returns internal module, like require. It's not public, so you shouldn't rely on it in your code, but you can use it to play with node's low level objects, if you want to understand how things work. For example, here timer_wrap binding is registered. It exports Timer constructor. In lib/timers.js it's imported https://stackoverflow.com/questions/24042861/nodejs-what-does-process-binding-mean 你可以直接看 process.binding('fs')就知道大概他是干嘛的了 |
10
kfll 2018-07-23 14:55:39 +08:00 1
1. require.resolve(x) === x
2. require.resolve.paths(x) === null 需要 8.9 3. process.binding(x) 私有接口,还包括一些不能 require 的内部模块 |