npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2025 – Pkg Stats / Ryan Hefner

batch-import

v3.2.0

Published

批量加载node.js模块、目录,并转换为对应的JS对象

Downloads

4

Readme

batch-import

批量加载指定目录下node.js模块,并将模块导出结果保存到与模块路径结构一致的对象中

install

  npm install batch-import

使用方法

let batchImport = require('batch-import')

let modules = batchImport(options, container)

batchImport(options, container)

  • options Object

    • path String - 模块所在相对路径(必填)

    • contain Array - 加载指定模块或目录,支持深度递归匹配,与exclude互斥(可选)

    • exclude Array - 排除指定模块或目录,支持深度递归匹配,与contain互斥(可选)

    • import(filename, data) Function - 模块导出数据处理函数,this指向当前层级容器。用于数据检验、预处理等操作(可选)

      • filename String - 当前文件名称,不含后缀

      • data * - 模块导出数据

    • complete(data) Function - 同一个配置项下的所有模块导出完成后的数据处理函数,this指向根容器。用于数据检验、预处理等操作(可选)

      • data Object - 所有子集模块导出数据集合
  • container Object - 可选,将模块导出结果保存到指定容器中

示例

let batchImport = require('batch-import')

let modules = batchImport({
   "middleware": {
      "path": "middleware/",
      "exclude": ["test.js"],
      import(filename, data) {
         if (data instanceof Function) {
            return data(this)
         } else {
            throw new Error(`${filename}模块导出必须为函数类型`)
         }
      },
      complete(data) {
         for (let name in data) {
            this[name] = data[name]
         }
         return data
      }
   },
   "models": {
      "path": "models/",
      import(filename, data) {
         if (data instanceof Function) {
            return data(this)
         } else {
            throw new Error(`${filename}模块导出必须为函数类型`)
         }
      },
   },
   "controllers": {
      "path": "controllers/",
      "contain": ["_route.js"],
   },
})

注意事项

  • 配置项加载顺序与定义顺序一致,配置越靠前优先级越高

  • path路径以入口文件所在位置作为根路径

  • path路径中不应该出现./、../等相对路径表达式