Registry / testing / babel-plugin-require-context-hook

babel-plugin-require-context-hook

JSON →
library1.0.0jsnpmunverified

A Babel plugin that replicates Webpack's require.context() for use in Node.js environments, primarily with Mocha tests or scripts. Current stable version 1.0.0. It rewrites require.context calls into a global function provided by a helper register script. Unlike alternatives (e.g., mock-require-context), this plugin uses a Babel transform to inject __dirname into the context, making it work seamlessly with babel-register. It has no runtime dependencies.

npm install babel-plugin-require-context-hook
INSTALL
IMPORT
SIG · BABEL-PLUGIN-REQUI
B
babel-plugin-require-context-hook
testingjavascriptv1.0.0
Install
Import
Disk
Pass rate
0/ 6
Env Coverage0 / 6
glibc
1822
musl
1822
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
node 18226 runs
build_error
glibc
node 18226 runs
build_error
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

register
require('babel-plugin-require-context-hook/register')();
import { register } from 'babel-plugin-require-context-hook/register'
The register module exports a function that must be called immediately; it is not a default export.
plugin
// in .babelrc: "plugins": ["require-context-hook"]
require('babel-plugin-require-context-hook').default
The plugin is a Babel plugin, not a direct JavaScript import. Add it to your Babel configuration.
require.context
const modules = require.context('./components', true, /\.js$/);
import { context } from 'babel-plugin-require-context-hook'
require.context is a Webpack feature; this plugin makes it available in Node by rewriting calls.

Shows installation, Babel configuration, registering the global helper, and using require.context() in a Node script.

// Install: npm install --save-dev babel-plugin-require-context-hook // .babelrc { "plugins": ["require-context-hook"] } // In your entry or test setup file (e.g., setup.js) require('babel-plugin-require-context-hook/register')(); // Then use require.context() anywhere, e.g., in a Mocha test const modules = require.context('./fixtures', true, /\.json$/); modules.keys().forEach(key => { const data = modules(key); console.log(data); });
Debug
Known issues
gotchaRequires calling the register function before any require.context usage; otherwise require.context is undefined.
fix
Ensure require('babel-plugin-require-context-hook/register')() is executed at the top of your entry point or in a setup file.
affects: all
breakingThe global require.context function is not available in the browser; only works in Node with babel-register.
fix
Use Webpack's native require.context for browser code.
affects: all
deprecatedNo known deprecations.
affects: >=0.0.0
Errors
Common errors & fixes
require.context is not a function
The register script was not called before trying to use require.context.
fix
Add require('babel-plugin-require-context-hook/register')() at the top of your entry file.
TypeError: Cannot read property 'keys' of undefined
The Babel plugin is not configured or the register step is missing.
fix
Check .babelrc includes 'require-context-hook' in plugins, and the register function is called.
Module not found: Error: Cannot resolve module 'babel-plugin-require-context-hook'
The package is not installed in devDependencies.
fix
Run npm install --save-dev babel-plugin-require-context-hook
Upgrade
Version history
1.0.0latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
12 hits · last 30 days
node
10
OpenAI (training)
1
Resources
babel-plugin-require-context-hook — npm install babel-plugin-require-context-hook · libregistry