关于nodejs的require顺序
![](https://common.cnblogs.com/images/copycode.gif)
---------------------------------------
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
![](https://common.cnblogs.com/images/copycode.gif)
简单来说,如果是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版本中发现不完全 ...
随机推荐
- spark第一篇--简介,应用场景和基本原理
摘要: spark的优势:(1)图计算,迭代计算(2)交互式查询计算 spark特点:(1)分布式并行计算框架(2)内存计算,不仅数据加载到内存,中间结果也存储内存 为了满足挖掘分析与交互式实时查询的 ...
- flex、As 3.0 小知识
<s:CheckBox id="checkBox" x="10" y="5" label="{new ObjectProx ...
- php数据排序---array_multisort
PHP代码 <?php $ar1 = array(10, 100, 100, 0); $ar2 = array(1, 3, 2, 4); array_multisort($ar1, $ar2); ...
- 剑指offer 旋转数组
class Solution { public: int minNumberInRotateArray(vector<int> rotateArray) { //常规的遍历方法时间是O(N ...
- ignite中的消息使用实例
消息很简单,跟其他的mq相仿,关键点: 一,添加监听,订阅主题 二,发送消息 写了一个简单的示例: Receiver.java package test.ignite.broadcast; impor ...
- Polycarp's problems
Polycarp's problems time limit per test 2 seconds memory limit per test 256 megabytes input standard ...
- UVALive 2678 大于s的最短子序列和
input n s 10<=n<=100000,s<1e9 a1 a2 ... an ai<=10000 output 大于s的最短子序列和的长度,没有输出0 #includ ...
- CodeForces 566B Replicating Processes
#include <bits/stdc++.h> #define N 3010 #define LL long long #define unsigned U using namespac ...
- 【转载】GDI 映像方式 之 SetViewportExtEx 与 SetWindowExtEx 解析
所谓视口代表设备,比如屏幕. 窗口代表我们的思维. 我们对windows说在(5,6)处画个点(调用GDI函数).windows认为是在我们的思维的(5,6)处画了个点.(也就是说5,6是逻辑坐标,G ...
- Windows、Linux -- 远程登录、文件传输、文件共享
Linux <---> Linux 远程登录: ssh服务 ssh root@10.20.62.124 文件传输: scp服务 上传 scp /home/xxx.txt root@1 ...