关于nodejs的require顺序

---------------------------------------
check /home/somebody/node_modules/othermodule
check /home/somebody/node_modules/othermodule.js
check /home/somebody/node_modules/othermodule.json
check /home/somebody/node_modules/othermodule.node
---------------------------------------
if /home/somebody/node_modules/othermodule/package.json exists
check /home/somebody/node_modules/othermodule/package.json[main]
---------------------------------------
if /home/somebody/node_modules/othermodule/index.js exists
check /home/somebody/node_modules/othermodule/index.js
---------------------------------------
if /home/somebody/node_modules/othermodule/index.node exists
check /home/somebody/node_modules/othermodule/index.node
---------------------------------------
check /home/node_modules/othermodule
check /home/node_modules/othermodule.js
check /home/node_modules/othermodule.json
check /home/node_modules/othermodule.node
---------------------------------------
if /home/node_modules/othermodule/package.json exists
check /home/node_modules/othermodule/package.json[main]
---------------------------------------
if /home/node_modules/othermodule/index.js exists
check /home/node_modules/othermodule/index.js
---------------------------------------
if /home/node_modules/othermodule/index.node exists
check /home/node_modules/othermodule/index.node
---------------------------------------
check /node_modules/othermodule
check /node_modules/othermodule.js
check /node_modules/othermodule.json
check /node_modules/othermodule.node
---------------------------------------
if /node_modules/othermodule/package.json exists
check /node_modules/othermodule/package.json[main]
---------------------------------------
if /node_modules/othermodule/index.js exists
check /node_modules/othermodule/index.js
---------------------------------------
if /node_modules/othermodule/index.node exists
check /node_modules/othermodule/index.node
--------------------------------------- for each $PATH in $NODE_PATH ---------------------------------------
if $PATH/package.json exists
check $PATH/package.json[main]
---------------------------------------
if $PATH/index.js exists
check $PATH/index.js
---------------------------------------
if $PATH/index.node exists
check $PATH/index.node
---------------------------------------
if $HOME/.node_modules/package.json exists
check $HOME/.node_modules/package.json[main]
---------------------------------------
if $HOME/.node_modules/index.js exists
check $HOME/.node_modules/index.js
---------------------------------------
if $HOME/.node_modules/index.node exists
check $HOME/.node_modules/index.node
---------------------------------------
if $HOME/.node_libraries/package.json exists
check $HOME/.node_libraries/package.json[main]
---------------------------------------
if $HOME/.node_libraries/index.js exists
check $HOME/.node_libraries/index.js
---------------------------------------
if $HOME/.node_libraries/index.node exists
check $HOME/.node_libraries/index.node
---------------------------------------
if $PREFIX/lib/node/package.json exists
check $PREFIX/lib/node/package.json[main]
---------------------------------------
if $PREFIX/lib/node/index.js exists
check $PREFIX/lib/node/index.js
---------------------------------------
if $PREFIX/lib/node/index.node exists
check $PREFIX/lib/node/index.node

简单来说,如果是require('x')这样开头不是相对or绝对地址符号,尾巴也没说是.js或者.json的,就当做模块来找。先找是不是core module,然后一级一级向上看node_modules文件夹,每一级的node_modules先看里面是否有basename为所找的文件,再看是否有模块名文件夹下package.json的main标明的文件,然后不死心地看看模块名文件夹下有没有index.js和index.node。最后找不到的话,还要搜一遍全局环境,比如$HOME/.node_modules/什么的。
关于nodejs的require顺序的更多相关文章
- [代码]解析nodejs的require,吃豆人的故事
最近在项目中需要对nodejs的require关键字做解析,并且替换require里的路径.一开始我希望nodejs既然作为脚本语言,内核提供一个官方的parser库应该是一个稳定可靠又灵活的渠道,然 ...
- nodejs开发——require与exports的使用
nodejs开发——require与exports的使用 另一片文章总结:http://www.cnblogs.com/hfultrastrong/p/8036682.html require req ...
- nodejs的require模块及路径
在nodejs中,模块大概可以分为核心模块和文件模块. 核心模块是被编译成二进制代码,引用的时候只需require表示符即可,如(require('net')). 文件模块,则是指js文件.json文 ...
- 学会Nodejs 的require和exports
NodeJs中模块的导入和导出,是最基本的概念,但是看了之后,心里还是有一些疑惑的地方,比如: 1.有两个文件:part.js 和 main.js,main.js中要想使用part.js中定义的变量和 ...
- NodeJs中require use get typescript及其他知识点集合
NodeJs的Express使用 nodejs事件的监听与事件的触发 TypeScript学习笔记 深入浅出Node.js Nodejs开发Office插件 类百度文库文档上传.转换和展示功能项目开源 ...
- nodejs的require是如何执行的
通常,在Node.js里导入是通过 require函数调用进行的. Node.js会根据 require的是相对路径还是非相对路径做出不同的行为. 相对路径 相对路径很简单. 例如,假设有一个文件路径 ...
- nodejs中 require 方法的加载规则
require参数类型 http.fs.path等,原生模块 ./mod或../mod,相对路径的文件模块 /pathtomodule/mod,绝对路径的文件模块 mod,非原生模块的文件模块 在进 ...
- nodejs module/require
1. wrap up a module using module.exports, name the file to a.js var fun1=function(){ var stylus = re ...
- nodejs中require的路径是一个文件夹时发生了什么
node中使用require的时候如果路径是一个文件夹时,或者特殊的情况require('..');require('.'); 这是node实战这本书里说的情况,但是我在node6.9版本中发现不完全 ...
随机推荐
- Spring Security教程
原文地址:http://blog.csdn.net/jaune161/article/details/17640071 http://blog.csdn.net/jaune161/article/de ...
- c# 快速验证代理IP是否有用
public void yanzhen(string str, int port) { WebProxy proxyObject = new WebProxy(str, port);//str为IP地 ...
- eclipse关联源码的方法
1.在项目的libs目录下,新建一个android-support-v4.jar.properties文件 2.打开android-support-v4.jar.properties,编辑. 输入源码 ...
- js里写网页结构, 传函数参数
如题 "<td align='center' height='30px' width='80px'><a href='javascript:sort(\"&quo ...
- JavaScript跨域总结与解决办法 什么是跨域
什么是跨域 1.document.domain+iframe的设置 2.动态创建script 3.利用iframe和location.hash 4.window.name实现的跨域数据传输 5.使用H ...
- Unity3d 物体沿着正七边形轨迹移动
不对之处,敬请谅解. 1.圆内接正七边形半径 public static float r = 10; 2.存储七个顶点的值 Vector3[] ar = new Vector3[7]; 3.圆心角 s ...
- SQL 列拆分
with CTE as( SELECT A.id, B.value FROM( SELECT id, value = CONVERT(xml,'<root><v>' + REP ...
- linux sort 用法
sort命令是帮我们依据不同的数据类型进行排序,其语法及常用参数格式: sort [-bcfMnrtk][源文件][-o 输出文件]补充说明:sort可针对文本文件的内容,以行为单位来排序. 参 数: ...
- 用GeneratedKeyHolder获得新建数据主键值
public User createUser(final User user) { final String sql = "insert into sys_users(username, p ...
- 【java图形计算器】 java awt swing组件应用
package package1; import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swi ...