Skip to content

说说 Node 文件查找的优先级以及 require 方法的文件查找策略? #121

Open
@TieMuZhen

Description

@TieMuZhen

查找策略

require方法接收一下几种参数的传递:

  • 原生模块:http、fs、path等
  • 相对路径的文件模块:./mod或../mod
  • 绝对路径的文件模块:/pathtomodule/mod
  • 目录作为模块:./dirname
  • 非原生模块的文件模块:mod

require参数较为简单,但是内部的加载却是十分复杂的,其加载优先级也各自不同,如下图:

从上图可以看见,文件模块存在缓存区,寻找模块路径的时候都会优先从缓存中加载已经存在的模块

参考文献

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions