Main property in package.json defines package entry point
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 exports
object 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.js
, index.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的更多相关文章
- Nodejs之package.json介绍说明
规范的package.json及package.json中各属性的作用. "name":包名. "description":包简介. "author& ...
- npm 与 package.json 快速入门
npm 是前端开发广泛使用的包管理工具,之前使用 Weex 时看了阮一峰前辈的文章了解了一些,这次结合官方文章总结一下,加深下理解吧! 读完本文你将了解: 什么是 npm 安装 npm 更新 npm ...
- package.json文档
之前在博客中写过一篇关于 " node.js的安装配置 " 的文章,里面有提到利用 gulp watch 来监听文档的变化.其中需要 package.json 文件才能实现效果,所 ...
- 关于 package.json 和 package-lock.json 文件说明
package.json 在 Node.js 中,模块是一个库或框架,也是一个 Node.js 项目.Node.js 项目遵循模块化的架构,当我们创建了一个 Node.js 项目,意味着创建了一个模块 ...
- npm 与 package.json 快速入门教程
npm 与 package.json 快速入门教程 2017年08月02日 19:16:20 阅读数:33887 npm 是前端开发广泛使用的包管理工具,之前使用 Weex 时看了阮一峰前辈的文章了解 ...
- GHOST CMS - Package.json
Package.json The package.json file is a set of meta data about a theme. package.json 文件是一组关于主题的元数据. ...
- [转载]npm 与 package.json 快速入门教程
npm 与 package.json 快速入门教程 2017-08-02 19:16:20 拭心 阅读数 78648更多 分类专栏: 学学前端 版权声明:本文为博主原创文章,遵循CC 4.0 BY ...
- Node.js NPM Package.json
章节 Node.js NPM 介绍 Node.js NPM 作用 Node.js NPM 包(Package) Node.js NPM 管理包 Node.js NPM Package.json Nod ...
- npm与package.json快速入门
本文转载自npm与package.json快速入门 导语 npm 是前端开发广泛使用的包管理工具,之前使用 Weex 时看了阮一峰前辈的文章了解了一些,这次结合官方文章总结一下,加深下理解吧! 读完本 ...
随机推荐
- 如何让类数组也使用数组的方法比如:forEach()
思路: 让类数组绑定数组的方法<div>1</div><div>2</div>方法一: let div = document.getElementsBy ...
- 30分钟用Restful ABAP Programming模型开发一个支持增删改查的Fiori应用
2016年时,Jerry曾经写过一系列关于SAP Fiori Smart Template(现在更名为Fiori Elements了)的博客,介绍了所谓的MDD开发方法论 - Metadata Dri ...
- 简述FTP的主动模式与被动模式(精简)
一.主被动 主动: 客户端从任意一个大于1024的端口现在假设为1234(非特权端口)连接到服务端的21端口(命令端口),随之客户端监听端口(N+1)即为1235端口(可以理解为这是客户端认定的数据端 ...
- Centos7.6进入挂载硬盘后,进入应急模式(emergency mode)而非图形模式解决方法
Centos7.6进入挂载硬盘后,进入应急模式(emergency mode)而非图形模式解决方法 话说某天我想在centos7.6中挂载个硬盘,结果刚在虚拟机中添加了一块硬盘,再次打开系统时,居然就 ...
- 爬虫:selenium请求库
一.介绍 二.安装 三.基本使用 四.选择器 五.等待元素被加载 六.元素交互操作 七.其他 八.项目练习 一.介绍 # selenium最初是一个自动化测试工具,而爬虫中使用它主要是为了解决requ ...
- 用 Redis 实现延时任务
原文:https://cloud.tencent.com/developer/article/1358266 1.什么是延时任务 延时任务,顾名思义,就是延迟一段时间后才执行的任务.延时任务的使用还是 ...
- JavaScript 常用Object
对象 - Map 功能:Map 对象保存键值对. 方法: new Map():新建一个 Map 对象 Map.prototype.has(key):返回布尔值.表示 Map 实例是否包含键对应的值. ...
- blind XXE payload
简单验证 POST /test HTTP/1.1 Content-Type: application/soap+xml User-Agent: scanner Accept: */* Cache-Co ...
- javaWeb上传
上传(上传不能使用BaseServlet) 1. 上传对表单限制 * method="post" * enctype="multipart/form-data&quo ...
- Java链接Redis时出现 “ERR Client sent AUTH, but no password is set”
Java链接Redis时出现 “ERR Client sent AUTH, but no password is set” 异常的原因及解决办法. [错误提示] redis.clients.jedis ...