官术网_书友最值得收藏!

Directories as modules

A module can contain a whole directory structure full of stuff. Stuff here is a technical term referring to internal file modules, data files, template files, documentation, tests, assets, and more. Once stored within a properly constructed directory structure, Node.js will treat these as a module that satisfies a require('moduleName') call.

This may be a little confusing because the word module is being overloaded with two meanings. In some cases, a module is a file, and in other cases, a module is a directory containing one or more file modules.

In most cases, a directory-as-module contains a package.json file. This file contains data about the module (known as package) that Node.js uses while loading the module. The Node.js runtime recognizes these two fields:

{ name: "myAwesomeLibrary", 
  main: "./lib/awesome.js" }

If this package.json file is in a directory named awesomelib, then require('./awesomelib') will load the file module in ./awesomelib/lib/awesome.js

If there is no package.json, then Node.js will look for either index.js or index.node. In such a case, require('./awesomelib') will load the file module in ./awesomelib/index.js.

In either case, the directory module can easily contain other file modules. The module that's initially loaded would simply use require('./anotherModule') one or more times to load other, private modules.

The npm package management system can recognize a lot more data in the package.json file. That includes the package name, its author, the home page URL, the issue-queue URL, package dependencies, and more. We'll go over this later.

主站蜘蛛池模板: 娄烦县| 隆尧县| 荆门市| 翁牛特旗| 涟源市| 古田县| 拜泉县| 蛟河市| 阿克| 左贡县| 澄江县| 中方县| 淮安市| 浦江县| 奉贤区| 延川县| 留坝县| 利川市| 潜山县| 穆棱市| 万全县| 邻水| 巴楚县| 西林县| 东辽县| 遂川县| 遵化市| 大理市| 方正县| 南郑县| 许昌市| 措勤县| 张家口市| 四平市| 镶黄旗| 德江县| 双江| 股票| 利辛县| 海盐县| 宁阳县|