What is import-modules?
The import-modules npm package is designed to dynamically import all modules from a specified directory. This can be particularly useful for loading multiple modules without having to manually require each one, streamlining the process of module management in Node.js applications.
What are import-modules's main functionalities?
Import all modules from a directory
This feature allows you to import all modules from a specified directory. The `importModules` function takes the path to the directory as an argument and returns an object containing all the modules in that directory.
const importModules = require('import-modules');
const modules = importModules('path/to/directory');
console.log(modules);
Filter modules by file extension
This feature allows you to filter the modules by file extension. By passing a filter option, you can specify which files to include, such as only JavaScript files.
const importModules = require('import-modules');
const jsModules = importModules('path/to/directory', { filter: '**/*.js' });
console.log(jsModules);
Custom module key
This feature allows you to customize the keys of the imported modules. By setting the `camelize` option to false, the keys will not be camelized, preserving the original file names.
const importModules = require('import-modules');
const modules = importModules('path/to/directory', { camelize: false });
console.log(modules);
Other packages similar to import-modules
require-all
The require-all package provides similar functionality by requiring all modules within a directory. It offers options for filtering files and customizing the module keys. Compared to import-modules, require-all is more focused on requiring modules rather than importing them, but the end result is quite similar.
glob
The glob package allows you to match files using patterns, which can then be required or imported manually. While it does not directly import modules, it provides powerful file matching capabilities that can be used in conjunction with require or import statements. This makes it more flexible but requires additional steps compared to import-modules.
readdirp
The readdirp package is a recursive version of fs.readdir, allowing you to read directories and their subdirectories. It can be used to find files and then require or import them manually. Like glob, it offers more flexibility but requires additional steps to achieve the same functionality as import-modules.
import-modules
Import all modules in a directory
This package is intentionally simple. Not interested in more features.
Install
$ npm install import-modules
Usage
.
└── directory
├── foo-bar.js
└── baz-faz.js
const importModules = require('import-modules');
const modules = importModules('directory');
console.log(modules);
API
importModules(directory?, options?)
directory
Type: string
Default: __dirname
Directory to import modules from. Unless you've set the fileExtensions
option, that means any .js
, .json
, .node
files, in that order. Does not recurse. Ignores the caller file and files starting with .
or _
.
options
Type: object
camelize
Type: boolean
Default: true
Convert dash-style names (foo-bar
) and snake-style names (foo_bar
) to camel-case (fooBar
).
fileExtensions
Type: string[]
Default: ['.js', '.json', '.node']
File extensions to look for. Order matters.
Related