Instead of:
const lib = {
foo: require('../../lib/foo'),
bar: require('../../lib/bar'),
...
}
use:
const req = require('require-all-root')
const lib = req('/lib')
require-all-root
can import individual files, modules and packages from /node_modules
.
const req = require('require-all-root')
// exteral modules
const fs = req('fs')
const express = req('express')
// if /module/index.js exists
const module = req('/module')
// equals to
const module = req('/module/index')
const module = req('/module/index.js')
// if /data.json exists
const data = req('/data')
To avoid require('require-all-root')
in multiple files, you can setup it ones in а global
object
// In start.js
global.req = require('require-all-root')
// In /some/path.js
const lib = req('/lib')
1.0.1
- Module inner errors are thrown
- Allows to require all for relative path
- Felix Geisendörfer for require-all
- Chris Morrell for app-root-path