- Node.js Web Development
- David Herron
- 260字
- 2021-06-25 21:54:04
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.
- 少兒人工智能趣味入門(mén):Scratch 3.0動(dòng)畫(huà)與游戲編程
- Web前端開(kāi)發(fā)技術(shù):HTML、CSS、JavaScript(第3版)
- Getting Started with React
- 摩登創(chuàng)客:與智能手機(jī)和平板電腦共舞
- Instant Zepto.js
- AngularJS深度剖析與最佳實(shí)踐
- Linux操作系統(tǒng)基礎(chǔ)案例教程
- Python面向?qū)ο缶幊蹋簶?gòu)建游戲和GUI
- 數(shù)據(jù)結(jié)構(gòu)案例教程(C/C++版)
- Mastering Data Mining with Python:Find patterns hidden in your data
- Hands-On JavaScript for Python Developers
- DB2SQL性能調(diào)優(yōu)秘笈
- Mastering JavaScript
- Applied Deep Learning with Python
- PHP從入門(mén)到精通(第7版)