I know my project's dependencies are installed under node_modules directory. But when I do require('lodash'), how does Node know which file to load?

"How does Node know which file to read when loading a module?"

Module loading works in two phases

When loading a dependency, module loading in Node.js works in two phases. This is in contrast to loading a core node module with require('fs') or a local module with require('./queue/mem.js'). A dependency is loaded in two phases 1) the right directory is looked up and 2) the entry point within that directory is located.

Finding directory

Node looks up all node_modules directories that are on the path from the calling file and the root of the filesystem. It starts from the directory containing the file with require call and works all the way up the directory hierarchy. Node keeps looking until it finds a directory name under node_modules directory matching that of the require call. If no directory provides a hit, Node will try few system directories as a last resort.

Locating entry point

After finding the directory Node tries a couple of strategies to determining the entry point of the package. The entry point is the file that is to be loaded and its exportsobject to be returned as the return value of the originating require call. First, Node looks for a package.json file and checks if it contains a main property. It will be used to point a file inside the package directory that will be the entry point. If main property does not exist, then Node tries in order index.jsindex.json and index.node.

What does entry point look like for popular packages

What is done inside a package, is then up to the module authors. There are a couple of ways to organize the entry point to a package. Here's how some of the popular npm modules do it.

Module Main property in package.json
lodash "lodash.js"
async "lib/async.js"
request "index.js"
underscore "underscore.js"
express  
commander "index"
debug "./node.js"
chalk  
bluebird "./js/release/bluebird.js"

Entry points of popular npm packages.

It can be seen that many packages define the main property and only few leave it to the default lookup convention. The express and chalk packages are the only ones to rely on index lookup. For request and commander packages it would not be necessary to be stating index as the entry point since it is be the default.

Main property in package.json defines package entry point的更多相关文章

  1. Nodejs之package.json介绍说明

    规范的package.json及package.json中各属性的作用. "name":包名. "description":包简介. "author& ...

  2. npm 与 package.json 快速入门

    npm 是前端开发广泛使用的包管理工具,之前使用 Weex 时看了阮一峰前辈的文章了解了一些,这次结合官方文章总结一下,加深下理解吧! 读完本文你将了解: 什么是 npm 安装 npm 更新 npm ...

  3. package.json文档

    之前在博客中写过一篇关于 " node.js的安装配置 " 的文章,里面有提到利用 gulp watch 来监听文档的变化.其中需要 package.json 文件才能实现效果,所 ...

  4. 关于 package.json 和 package-lock.json 文件说明

    package.json 在 Node.js 中,模块是一个库或框架,也是一个 Node.js 项目.Node.js 项目遵循模块化的架构,当我们创建了一个 Node.js 项目,意味着创建了一个模块 ...

  5. npm 与 package.json 快速入门教程

    npm 与 package.json 快速入门教程 2017年08月02日 19:16:20 阅读数:33887 npm 是前端开发广泛使用的包管理工具,之前使用 Weex 时看了阮一峰前辈的文章了解 ...

  6. GHOST CMS - Package.json

    Package.json The package.json file is a set of meta data about a theme. package.json 文件是一组关于主题的元数据. ...

  7. [转载]npm 与 package.json 快速入门教程

    npm 与 package.json 快速入门教程 2017-08-02 19:16:20 拭心 阅读数 78648更多 分类专栏: 学学前端   版权声明:本文为博主原创文章,遵循CC 4.0 BY ...

  8. Node.js NPM Package.json

    章节 Node.js NPM 介绍 Node.js NPM 作用 Node.js NPM 包(Package) Node.js NPM 管理包 Node.js NPM Package.json Nod ...

  9. npm与package.json快速入门

    本文转载自npm与package.json快速入门 导语 npm 是前端开发广泛使用的包管理工具,之前使用 Weex 时看了阮一峰前辈的文章了解了一些,这次结合官方文章总结一下,加深下理解吧! 读完本 ...

随机推荐

  1. 前端面试记录NO.1

    后端转前端,离职后第一次面试,技术面试的时候还是比较虚的,因为基础不是很扎实.主要问了工作中用过哪些技术,主流框架的区别,jQuery的掌握情况,Ajax的掌握情况,cookie的基本内容,还有浏览器 ...

  2. 【转载】linux如何将新硬盘挂载到home目录下

    举例说明: 新增磁盘的设备文件名为 /dev/vdb 大小为100GB. #fdisk -l 查看新增的的磁盘 1.对新增磁盘进行分区 #fdisk /dev/vdb 按提示操作 p打印 n新增 d ...

  3. Python-共享引用

    A会改变么? 下面三小段代码,A的值都会改变么? >>> A = "spam" >>> B = A >>> B = " ...

  4. Linux命令——tac、rev

    tac和rev命令列到一起,并不是功能相似,而是他们都是将输入内容反置. tac -s:使用指定字符串代替换行作为分隔标志 [root@localhost ~]# echo "1,2&quo ...

  5. 剖析.o文件ELF组成

    ELF文件结构组成 ①总共13个节 ②每个节都有一个编号.从ELF头开始编号,编号从0开始,编号的作用就是用来索引(找到)不同节的. ③每个.o的都是这样的结构.链接时要做的就是,将ELF格式的.o全 ...

  6. KVM虚拟机的管理

    1.  查看KVM虚拟机配置文件及运行状态 (1) KVM虚拟机默认配置文件位置: /etc/libvirt/qemu/ autostart目录是配置kvm虚拟机开机自启动目录 (2) virsh命令 ...

  7. vim配置笔记

    1. vim两种配置方法 1)配置文件 全局配置文件:/etc/vim/vimrc或者/etc/vimrc 用户个人配置文件:~/.vimrc 2)命令模式 命令行模式下直接输入配置命令即可.如:se ...

  8. VSCode安装程序——java开发

    文章:微软为 Java 开发者推出 VSCode 安装程序 文章介绍微软为VSCode提供了开发程序,方便java开发者更好的使用VSCode

  9. shell中判断前一个命令是否执行成功

    ]; then echo "fail" else echo "success" fi 或者 ]; then echo "success" e ...

  10. 2星|项立刚《5G时代》:资料堆砌和一些假想设想,信息浓度太低

    “ 这是一本关于5G的书,但着眼点不是要说清楚5G的技术,因为解读5G技术的图书已经有很多,我自己也不是技术专家.本书是希望探讨在一个全新的网络体系下产业的发展与改变,以及5G对社会与经济的影响.P6 ...