Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

require-tree

Package Overview
Dependencies
Maintainers
1
Versions
8
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

require-tree

require() whole directory trees

  • 0.2.0
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
3.1K
decreased by-39.66%
Maintainers
1
Weekly downloads
 
Created
Source

require-tree

Create an object tree from file directories. Useful for mapping API trees or applications models/controllers.

Install

npm install require-tree

Usage

var require_tree = require('require-tree')

Assuming you have a file structure like this:

/models
  user.js
  page.js
  item.js

You can require all models at once:

require_tree('./models')

/* => {
    user: [object Object],
    page: [object Object],
    item: [object Object]
} */

It works with nested directories:

/api
  /pages
    index.js
    edit.js
  user.js

// api/pages/index.js:
module.exports = {
    list: function(){ ... }
}

// api/pages/edit.js:
module.exports = {
    getPermissions: function(){ ... },
    remove: function(){ ... }
}

// api/user.js:
module.exports = {
    profile: function(){ ... },
    posts: function(){ ... }
}

var pages = require_tree('./api')

/* Result:
    pages.list
    pages.edit.getPermissions
    pages.edit.remove
    pages.user.profile
    pages.user.posts
*/

To require the current module's root folder, in case you are using a directory's index.js to load the rest of the tree, use __dirname:

var tree = require_tree(__dirname, { index: false })

Options

{
    name  : [String | Function(exports, file)]         // the object's property to use as key
    main  : [String | Array | Function(exports, file)] // what keys should be exported
    index : [Boolean]                                  // load 'index.js' files (`true` by default)
    each  : [Function]                                 // callback to run after each module
}

Keywords

FAQs

Package last updated on 28 Mar 2013

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc