Created
February 18, 2022 14:11
-
-
Save YangShunGit/dbd2219ec177727dac59037e06adc2ee to your computer and use it in GitHub Desktop.
require 目录的机制
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 目录的机制是: | |
如果目录下有 package.json 并指定了 main 字段,则用之 | |
如果不存在 package.json,则依次尝试加载目录下的 index.js 和 index.node | |
require 过的文件会加载到缓存,所以多次 require 同一个文件(模块)不会重复加载 | |
判断是否是程序的入口文件有两种方式: | |
require.main === module(推荐) | |
module.parent === null |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment