require-directory

中文文档
2.1.1发布于 2015/05/28MITCommonJS零依赖近 30 天下载 5.5亿

Recursively iterates over specified directory, require()'ing each file, and returning a nested hash structure containing those modules.

原始使用文档
文档目录13 节
  1. How To Use
  2. Installation (via npm )
  3. Usage
  4. Specifying Another Directory
  5. Options
  6. Whitelisting
  7. Blacklisting
  8. Visiting Objects As They're Loaded
  9. Renaming Keys
  10. No Recursion
  11. Run Unit Tests
  12. License
  13. Author

require-directory

Recursively iterates over specified directory, require()'ing each file, and returning a nested hash structure containing those modules.

Follow me (@troygoode) on Twitter!

NPM

build status

How To Use

Installation (via npm)

$ npm install require-directory

Usage

A common pattern in node.js is to include an index file which creates a hash of the files in its current directory. Given a directory structure like so:

  • app.js
  • routes/
    • index.js
    • home.js
    • auth/
      • login.js
      • logout.js
      • register.js

routes/index.js uses require-directory to build the hash (rather than doing so manually) like so:

var requireDirectory = require('require-directory');
module.exports = requireDirectory(module);

app.js references routes/index.js like any other module, but it now has a hash/tree of the exports from the ./routes/ directory:

var routes = require('./routes');

// snip

app.get('/', routes.home);
app.get('/register', routes.auth.register);
app.get('/login', routes.auth.login);
app.get('/logout', routes.auth.logout);

The routes variable above is the equivalent of this:

var routes = {
  home: require('routes/home.js'),
  auth: {
    login: require('routes/auth/login.js'),
    logout: require('routes/auth/logout.js'),
    register: require('routes/auth/register.js')
  }
};

Note that routes.index will be undefined as you would hope.

Specifying Another Directory

You can specify which directory you want to build a tree of (if it isn't the current directory for whatever reason) by passing it as the second parameter. Not specifying the path (requireDirectory(module)) is the equivelant of requireDirectory(module, __dirname):

var requireDirectory = require('require-directory');
module.exports = requireDirectory(module, './some/subdirectory');

For example, in the example in the Usage section we could have avoided creating routes/index.js and instead changed the first lines of app.js to:

var requireDirectory = require('require-directory');
var routes = requireDirectory(module, './routes');

Options

You can pass an options hash to require-directory as the 2nd parameter (or 3rd if you're passing the path to another directory as the 2nd parameter already). Here are the available options:

Whitelisting

Whitelisting (either via RegExp or function) allows you to specify that only certain files be loaded.

var requireDirectory = require('require-directory'),
  whitelist = /onlyinclude.js$/,
  hash = requireDirectory(module, {include: whitelist});
var requireDirectory = require('require-directory'),
  check = function(path){
    if(/onlyinclude.js$/.test(path)){
      return true; // don't include
    }else{
      return false; // go ahead and include
    }
  },
  hash = requireDirectory(module, {include: check});

Blacklisting

Blacklisting (either via RegExp or function) allows you to specify that all but certain files should be loaded.

var requireDirectory = require('require-directory'),
  blacklist = /dontinclude\.js$/,
  hash = requireDirectory(module, {exclude: blacklist});
var requireDirectory = require('require-directory'),
  check = function(path){
    if(/dontinclude\.js$/.test(path)){
      return false; // don't include
    }else{
      return true; // go ahead and include
    }
  },
  hash = requireDirectory(module, {exclude: check});

Visiting Objects As They're Loaded

require-directory takes a function as the visit option that will be called for each module that is added to module.exports.

var requireDirectory = require('require-directory'),
  visitor = function(obj) {
    console.log(obj); // will be called for every module that is loaded
  },
  hash = requireDirectory(module, {visit: visitor});

The visitor can also transform the objects by returning a value:

var requireDirectory = require('require-directory'),
  visitor = function(obj) {
    return obj(new Date());
  },
  hash = requireDirectory(module, {visit: visitor});

Renaming Keys

var requireDirectory = require('require-directory'),
  renamer = function(name) {
    return name.toUpperCase();
  },
  hash = requireDirectory(module, {rename: renamer});

No Recursion

var requireDirectory = require('require-directory'),
  hash = requireDirectory(module, {recurse: false});

Run Unit Tests

$ npm run lint
$ npm test

License

MIT License

Author

Troy Goode ([email protected])

版本与发布记录

require-directory 共发布 12 个版本,首次发布于 2011/12/27;过去 12 个月发布了 0 个版本。

版本发布日期距今
2.1.1latest2015/05/2811 年前
2.1.02015/02/1911 年前
2.0.02014/07/2912 年前
1.2.02013/11/1412 年前
1.1.12013/09/0113 年前
1.1.02013/08/2913 年前

关于 require-directory 的常见问题

require-directory 是什么?

Recursively iterates over specified directory, require()'ing each file, and returning a nested hash structure containing those modules.

如何安装 require-directory?

在项目目录运行 npm install require-directory,或使用 pnpm add / yarn add / bun add。

require-directory 支持 TypeScript 吗?

该包的 package.json 没有声明类型入口。需要类型提示时,可以在 npm 上查找社区维护的 @types/require-directory 是否存在。

require-directory 支持 ESM 还是 CommonJS?

以 CommonJS 形式发布,可以直接 require();在 ES Module 项目中也可以用 import 引入(Node.js 会把 module.exports 作为默认导出)。

require-directory 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js >=0.10.0。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

require-directory 可以商用吗?

require-directory 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

require-directory 的下载量有多少?

根据 npm 官方下载统计,require-directory 在 2026/10/05 – 2026/11/03 这 30 天内被下载 552,227,722 次,日均约 18,407,591 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

require-directory 还在维护吗?

最新版本 2.1.1 发布于 2015/05/28(11 年前);至今共发布 12 个版本,其中过去 12 个月发布了 0 个;首次发布于 2011/12/27。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。