Security News
Fluent Assertions Faces Backlash After Abandoning Open Source Licensing
Fluent Assertions is facing backlash after dropping the Apache license for a commercial model, leaving users blindsided and questioning contributor rights.
require-tree
Advanced tools
A require()
-like method for directories, returning an object that mirrors the file tree.
npm install require-tree
Considering this file structure:
Requiring the models
directory will return an object containing each exported module:
var require_tree = require('require-tree')
require_tree('./models')
/* {
user: [object Object],
page: [object Object],
item: [object Object]
} */
Directories can be deeply nested, andindex.js
files are merged into their parent by default:
// api/user.js:
module.exports = {
profile: function(){},
posts: function(){}
}
// api/pages/index.js:
module.exports = {
list: function(){}
}
// api/pages/edit.js:
module.exports = {
getPermissions: function(){},
remove: function(){}
}
var api = require_tree('./api')
This will yield
api.user.profile
api.user.posts
api.pages.list
api.pages.edit.getPermissions
api.pages.edit.remove
require_tree(path, { options })
Use a property of the exports object as it's key (instead of the filename) in the final object.
// models/user-model.js
module.exports = {
id: 'user',
attrs: {}
}
require_tree('./models', { name: 'id' })
require_tree('./models', { name: function (obj) { return obj.id } })
// => { user: { id: 'user', attrs: {} } }
Filter the required files. Strings can use a wildcard '*' and are expanded into regular expressions. You can also provide your own RegExp, or a function that receives the filename as an argument, and returns true
or false
.
require_tree('./path', { filter: '*-model' })
require_tree('./path', { filter: /^model/ })
require_tree('./path', { filter: function (filename) { return filename.indexOf('model') === 0 } })
Use to return only certain keys from exported objects.
require_tree('./models', { keys: 'at*' })
require_tree('./models', { keys: ['attrs'] })
require_tree('./models', { keys: function (key){ return key.indexOf('attrs') >= 0 } })
// => { user: { attrs: {} } }
Callback to run after each file is required. Doesn't modify the exported object.
require_tree('./items', { each: function (obj) { items.insert(obj) } })
Same as each
, but can modify the exports object.
require_tree('./models', { transform: function (obj) { return new Model(obj) } })
merge
(default): merges the index.js
exports at the root of it's parentignore
: causes index.js
files to not be loaded at allpreserve
: puts the index.js
export object under the .index
propertyFor backwards compatibility, a value of true
is equal to preserve
, while false
is equal to ignore
.
// controllers/index.js:
module.exports = {
init: function () { ... }
}
var controllers = require_tree('./controllers', { index: 'preserve' })
controllers.index.init()
var controllers = require_tree('./controllers', { index: 'ignore' })
controllers.index // undefined
var controllers = require_tree('./controllers', { index: 'merge' })
controllers.init()
require-tree
must always be required in the local scope, never shared between modules or as a global. Paths are resolved relative to the parent module, like require
itself, so it's behaviour depends on module.parent
being set correctly. If necessary, you can use absolute paths (__dirname + '/path'
) or set the NODE_PATH
environment variable.
FAQs
Batch module loader, require() for directories
The npm package require-tree receives a total of 6,186 weekly downloads. As such, require-tree popularity was classified as popular.
We found that require-tree demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
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.
Security News
Fluent Assertions is facing backlash after dropping the Apache license for a commercial model, leaving users blindsided and questioning contributor rights.
Research
Security News
Socket researchers uncover the risks of a malicious Python package targeting Discord developers.
Security News
The UK is proposing a bold ban on ransomware payments by public entities to disrupt cybercrime, protect critical services, and lead global cybersecurity efforts.