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 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.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 时看了阮一峰前辈的文章了解了一些,这次结合官方文章总结一下,加深下理解吧! 读完本 ...
随机推荐
- Linux 之 文件
文件名称 在linux中,windows概念中的文件夹和文件是没有区别的,都是统称为文件. 1.Linux中文件的名称大小写是敏感的 2.名称最多可以为255个字符 3.除了正斜线以外,都是有效字符 ...
- NumPy 百题大冲关,冲鸭!
角色名称:NumPy 角色描述:NumPy是一个NASA都在用的扩展库. NumPy提供了许多高级的数值编程技能,如:矩阵数据类型.矢量处理,以及精密的运算库.专为进行严格的数字处理而战斗.是很多大型 ...
- Java学习笔记——第1篇
Java程序运行机制 Java程序要经过先编译,后解释两个步骤 编译型:高级语言源码 -->机器码(一次性) 生成可执行程序,脱离开发环境,在指定平台上运行.(C/C++/Objectiv ...
- TortoiseSVN “*/SendRpt.exe not found”?
- linux网络编程之socket编程(六)
经过一个国庆长假,又有一段时间没有写博文了,今天继续对linux网络编程进行学习,如今的北京又全面进入雾霾天气了,让我突然想到了一句名句:“真爱生活,珍惜生命”,好了,言归正传. 回顾一下我们之间实现 ...
- 哈希表(python)
# -*- coding: utf-8 -*- class Array(object): def __init__(self, size=32, init=None): self._size = si ...
- 第八届蓝桥杯C/C++程序设计本科B组决赛 ——瓷砖样式(填空题)【DP?我的暴力排列搜索】
标题:磁砖样式 小明家的一面装饰墙原来是 3*10 的小方格. 现在手头有一批刚好能盖住2个小方格的长方形瓷砖. 瓷砖只有两种颜色:黄色和橙色. 小明想知道,对于这么简陋的原料,可以贴出多少种不同的花 ...
- JDK源码那些事儿之ArrayBlockingQueue
线程在JDK中是非常重要的一块内容,所有的应用服务都离不开线程的相关操作,对于大量线程的使用都是推荐使用线程池来进行操作和管理,JDK本身提供的线程池在服务中经常被使用到,以往经常使用Executor ...
- Robot Framework--pybot命令
1.执行整个项目下的所有用例: pybot 项目路径.例如: pybot D:\robotPS:robot项目里面所有用例 2.执行某个suit中的所有用例: pybot 项目路径\suit文件名称. ...
- machine learning(8) -- classification
分类预测不能使用linear regression, linear regression算法对于分类预测效果很差,应使用logistic regression算法 Logistic regressti ...