gpt4 book ai didi

node.js - NativeModule 是如何工作的?它们是否被缓存?

转载 作者:行者123 更新时间:2023-12-05 05:51:20 25 4
gpt4 key购买 nike

require('http')
console.log(require.cache.http) // undefined

我在 require.cache 中找不到 http 模块?我怎样才能检测到它是必需的?我发现 process.moduleLoadList 包含字符串 NativeModule http 但没有看到任何对缓存模块的引用它在哪里?

最佳答案

NativeModule 意味着它没有被缓存:它被编译了。是这样定义的,

NativeModule: a minimal module system used to load the JavaScript core modules found in lib/**/*.js and deps/**/*.js. All core modules are compiled into the node binary via node_javascript.cc generated by js2c.py, so they can be loaded faster without the cost of I/O. This class makes the lib/internal/*, deps/internal/* modules and internalBinding() available by default to core modules, and lets the core modules require itself via require('internal/bootstrap/loaders') even when this file is not written in CommonJS style.

强调“没有 I/O 成本”,因此不需要缓存。

您可以在 lib/internal/bootstrap/loaders.js 中找到该系统.您可以找到 mapping of native modules to source code here ,但请记住它们不是从磁盘读取的。你可以找到js2c.py

关于node.js - NativeModule 是如何工作的?它们是否被缓存?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70392477/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com