webpack-requiredir
raw JSON → 0.2.1 verified Sat Apr 25 auth: no javascript deprecated
This library provides a simple function to recursively require all modules from a webpack require.context, returning a hash of modules keyed by their names. Version 0.2.1 is the latest. It supports optional object modification, exclusion of specific modules, and applying a transformation function to each module. It is a minimal utility for use with webpack's require.context, primarily in Node.js or webpack-bundled applications. No active development; last updated in 2016.
Common errors
error Cannot find module 'webpack-requiredir' ↓
cause Package may not be installed or the name is misspelled.
fix
Run: npm install webpack-requiredir
error TypeError: require.context is not a function ↓
cause Code is not running in a webpack-bundled environment (e.g., Node.js outside webpack).
fix
Ensure the code is part of a webpack bundle, or use a polyfill for require.context.
error Unexpected token export ↓
cause The package uses CommonJS, but your bundler expects ESM imports.
fix
Use require() instead of import, or configure your bundler to handle CommonJS.
Warnings
deprecated Package is no longer maintained; last update in 2016. Use dynamic imports or modern webpack context features instead. ↓
fix Replace with manual import() calls or webpack's require.context directly with custom logic.
gotcha require.context is webpack-specific; this library only works in a webpack-bundled environment. ↓
fix Ensure code is bundled with webpack; use only where require.context is available.
gotcha The resolved module keys are file paths relative to the context directory; not suitable for dynamic access without normalization. ↓
fix Map keys as needed (e.g., strip extensions and leading './').
Install
npm install webpack-requiredir yarn add webpack-requiredir pnpm add webpack-requiredir Imports
- default wrong
import requireDir from 'webpack-requiredir';correctconst requireDir = require('webpack-requiredir'); - requireDir wrong
import { requireDir } from 'webpack-requiredir';correctconst requireDir = require('webpack-requiredir'); requireDir(require.context('./src', true, /\.js$/)); - require.context wrong
const result = requireDir(require.context('./dir', true, /\.js$/), {});correctconst result = requireDir(require.context('./dir', true, /\.js$/));
Quickstart
const requireDir = require('webpack-requiredir');
const context = require.context('./src/components', true, /\.js$/);
const modules = requireDir(context, {
exclude: ['index.js'],
functionToApply: (module) => module.default || module
});
console.log(modules);