nodejs文件引用规则

nodejs文件引用规则

 1、node优先加载核心模块,如http、net、stream等,如果用户自定义模块与node模块全名冲突,则会加载的是核心模块。

 2、如果文件名以"/"、"./"、或"../"打头,则node直接去加载文件,如果没文件,则去加载dir。注意在查找文件的时候,如果失败,会自动给文件加上.js、.json、.node文件。

  

  

 3、如果2依旧失败,则在node_module路径下递归的查找。

  

4、Additionally, Node.js will search in the following list of GLOBAL_FOLDERS:

  • 1: $HOME/.node_modules
  • 2: $HOME/.node_libraries
  • 3: $PREFIX/lib/node

  Where $HOME is the user's home directory, and $PREFIX is Node.js's configured node_prefix.

  It is strongly encouraged to place dependencies in the local node_modules folder. These will be loaded faster, and more reliably.

原文地址:https://www.cnblogs.com/tekkaman/p/3009874.html