require.context() allows you to create a context to dynamically require a set of modules matching specific criteria.
Syntax
Parameters
Directory to search in (relative to current file).
Whether to search subdirectories.
Regular expression to match files against.
Module loading mode.Values:
sync | lazy | weak | eager | lazy-onceReturn Type
Returns:WebpackContext function with properties:
keys(): Returns array of all possible module pathsresolve(key): Returns module ID for a given keyid: Context module ID
Basic Usage
Loading All Files in Directory
Auto-importing Components
Loading Locale Files
Advanced Patterns
Vue Component Auto-registration
Vuex Module Auto-loading
SVG Icon Registry
Plugin System
Lazy Loading with Context
Lazy Mode
Lazy-Once Mode
Context API Methods
keys()
Returns an array of all matching module paths.resolve(key)
Returns the module ID for a given key.id
The module ID of the context itself.Dynamic Contexts
Building Module Maps
Conditional Loading
React Examples
Auto-import Redux Reducers
Image Importing
Performance Considerations
Limit Context Scope
Use Lazy Loading for Large Sets
TypeScript Support
Common Patterns
Test File Discovery
Asset Manifest
See Also
- import() - Dynamic imports
- require() - CommonJS require
- Module Variables - Module-scoped variables
- Dependency Management - Managing dependencies