
node.js极速入门课程:进入学习
相信大家都知道如何在 Node 中加载一个模块:
const fs = require('fs'); const express = require('express'); const anotherModule = require('./another-module');
没错,require
就是加载 cjs 模块的 API,但 V8 本身是没有 cjs 模块系统的,所以 node 是怎么通过 require
找到模块并且加载的呢?【相关教程推荐:nodejs视频教程】
我们今天将对 Node.js 源码进行探索,深入理解 cjs 模块的加载过程。 我们阅读的 node 代码版本为 v17.x:
- git head :881174e016d6c27b20c70111e6eae2296b6c6293
- 代码链接:github.com/nodejs/node…
源码阅读
内置模块
为了知道 require
的工作逻辑,我们需要先了解内置模块是如何被加载到 node 中的(诸如 'fs','path','child_process',其中也包括一些无法被用户引用的内部模块),准备好代码之后,我们首先要从 node 启动开始阅读。
node 的 main 函数在 [src/node_main.cc](https://github.com/nodejs/node/blob/881174e016d6c27b20c70111e6eae2296b6c6293/src/node_main.cc#L105)
内,通过调用方法 [node::Start](https://github.com/nodejs/node/blob/881174e016d6c27b20c70111e6eae2296b6c6293/src/node.cc#L1134)
来启动一个 node 实例:
int Start(int argc, char** argv) { InitializationResult result = InitializeOncePerProcess(argc, argv); if (result.early_return) { return result.exit_code; } { Isolate::CreateParams params; const std::vector<size_t>* indices = nullptr; const EnvSerializeInfo* env_info = nullptr; bool use_node_snapshot = per_process::cli_options->per_isolate->node_snapshot; if (use_node_snapshot) { v8::StartupData* blob = NodeMainInstance::GetEmbeddedSnapshotBlob(); if (blob != nullptr) { params.snapshot_blob = blob; indices = NodeMainInstance::GetIsolateDataIndices(); env_info = NodeMainInstance::GetEnvSerializeInfo(); } } uv_loop_configure(uv_default_loop(), UV_METRICS_IDLE_TIME); NodeMainInstance main_instance(¶ms, uv_default_loop(), per_process::v8_platform.Platform(), result.args, result.exec_args, indices); result.exit_code = main_instance.Run(env_info); } TearDownOncePerProcess(); return result.exit_code; }
这里创建了事件循环,且创建了一个 NodeMainInstance
的实例 main_instance
并调用了它的 Run 方法:
int NodeMainInstance::Run(const EnvSerializeInfo* env_info) { Locker locker(isolate_); Isolate::Scope isolate_scope(isolate_); HandleScope handle_scope(isolate_); int exit_code = 0; DeleteFnPtr<Environment, FreeEnvironment> env = CreateMainEnvironment(&exit_code, env_info); CHECK_NOT_NULL(env); Context::Scope context_scope(env->context()); Run(&exit_code, env.get()); return exit_code; }