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.3.2
  • Source
  • npm
  • Socket score

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

require-tree

NPM badge

Require multiple files at once, creating an object tree that mirrors the directory structure.

npm install require-tree

Usage

var require_tree = require('require-tree')

// File structure:
// ./models
//    user.js
//    page.js
//    item.js

require_tree('./models')

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

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 api = require_tree('./api')

// api.pages.list
// api.pages.edit.getPermissions
// api.pages.edit.remove
// api.user.profile
// api.user.posts

Limitations

Since v0.3 require-tree will resolve paths relative to the requiring module, like require itself.

Since it depends on module.parent being set correctly, either require-tree must be explicitly required within the current module scope, or you need to provide an absolute path like __dirname + '/somepath'.

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)
    filter : [String | RegExp | Function]               // filter pattern
    each   : [Function]                                 // callback to run after each module
}

Keywords

FAQs

Package last updated on 31 Oct 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