
Security News
Attackers Are Hunting High-Impact Node.js Maintainers in a Coordinated Social Engineering Campaign
Multiple high-impact npm maintainers confirm they have been targeted in the same social engineering campaign that compromised Axios.
Node.JS module which allows you to require all of the files in an entire
directory with a single call to require.
The default behavior of the Node.JS require function when you give it a
directory path, for example require('./mymodules'), is to look for an
index.js file in the relative directory mymodules.
That is, it will load ./mymodules/index.js This is useful, but sometimes
you kind of wish that it imported all of the scripts in that directory instead.
Suppose I have a directory structure like so:
* app.js // contains code dependent on mymodules
-mymodules // directory
* first.js // contains -- module.exports = { name: 'first' }
* second.js // contains -- module.exports = { name: 'second' }
- third // directory
* index.js // contains -- module.exports = { name: 'third' }
- more // directory
* fourth.js // contains -- module.exports = { name: 'fourth' }
* fifth.js // contains -- module.exports = { name: 'fifth' }
The default behavior of Node.JS when you call require on mymodules would be
to only load index.js. You could fix this by having index.js import all of
its sibling files and directories, but everytime you add or remove a file you
would have to go back update this index.js.
Sometimes I would like to be able to call require on mymodules and have it
return an object such as below with properties for each of the modules it
contains. I don't want to have to update an index.js everytime I add or
remove something from mymodules.
{
first: {
name: 'first'
},
second: {
name: 'second'
},
third: {
name: 'third'
},
more: {
fourth: {
name: 'fourth'
},
fifth: {
name: 'fifth'
}
}
}
You should include export-dir as a dependency in your package.json file, you
may do so by runnning:
npm install export-dir --save
In order to get an object like that in the example above when requiring a
directory we will still need to add an index.js file to it. However, it only
contains 2 lines which do not change and we don't have to update it when we
add or remove things from the directory.
var exportDir = require('export-dir');
module.exports = exportDir(__dirname);
In the above example you would need to create an index.js file like this in
both the ./mymodules directory and in ./mymodules/more in order to get
the exported object shown above.
Note that because export-dir creates an object with properties based on the
file and directory names exported you will not be able to use filenames that
are not valid JavaScript property names. For example my-module must become
myModule or something similar.
This project uses the mocha module for testing. This dependencies is only for development and is not needed for use of export-dir. In order to install the development dependencies and run the tests using npm please run:
npm install -d
npm test
export-dir currently consists of a single function which expects a first
argument to be a string which points to the path of the directory containing
modules to be exported. The path argument can be input using __dirname.
The function may also accept an optional second parameter which is an object with properties enabling or disabling options. The currently are shown here:
// index.js -- do not export any json files in the current directory. module.exports = exportDir(__dirname, { excludeJSON: true });
FAQs
Tool to require all files in a single directory in Node.JS
We found that export-dir 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
Multiple high-impact npm maintainers confirm they have been targeted in the same social engineering campaign that compromised Axios.

Security News
Axios compromise traced to social engineering, showing how attacks on maintainers can bypass controls and expose the broader software supply chain.

Security News
Node.js has paused its bug bounty program after funding ended, removing payouts for vulnerability reports but keeping its security process unchanged.