{"id":19155,"library":"babel-plugin-require-context-hook-babel7","title":"babel-plugin-require-context-hook","description":"A Babel plugin that replicates Webpack's `require.context` functionality for use with `babel-register`. Version 1.0.0 is stable; the plugin rewrites `require.context()` calls to use a global polyfill that must be registered at runtime via `require('babel-plugin-require-context-hook/register')()`. It depends on `@babel/core ^7.2.2`. This is a minimal, focused alternative to full Webpack configurations for projects needing dynamic require in Node.js.","status":"active","version":"1.0.0","language":"javascript","source_language":"en","source_url":"https://github.com/smrq/babel-plugin-require-context-hook","tags":["javascript","babel","webpack"],"install":[{"cmd":"npm install babel-plugin-require-context-hook-babel7","lang":"bash","label":"npm"},{"cmd":"yarn add babel-plugin-require-context-hook-babel7","lang":"bash","label":"yarn"},{"cmd":"pnpm add babel-plugin-require-context-hook-babel7","lang":"bash","label":"pnpm"}],"dependencies":[{"reason":"Required as a peer dependency for Babel plugin functionality.","package":"@babel/core","optional":false}],"imports":[{"note":"The register script must be required and called as a function. ES imports will not invoke the function.","wrong":"import 'babel-plugin-require-context-hook/register'","symbol":"ContextPolyfill","correct":"require('babel-plugin-require-context-hook/register')()"},{"note":"All three arguments are required in the polyfill; the original Webpack signature allows omitting. The plugin rewrites calls to include __dirname.","wrong":"require.context(directory)","symbol":"require.context","correct":"require.context(directory, useSubdirectories, regExp)"},{"note":"In .babelrc, use the short name (without 'babel-plugin-') to avoid double prefixing.","wrong":"\"plugins\": [\"babel-plugin-require-context-hook\"]","symbol":"babel-plugin syntax","correct":"\"plugins\": [\"require-context-hook\"]"}],"quickstart":{"code":"// .babelrc file\n{\n  \"plugins\": [\"require-context-hook\"]\n}\n\n// entry.js - must run before any module that uses require.context\nrequire('babel-plugin-require-context-hook/register')();\n\n// usage in a module\nconst modules = require.context('./components', true, /\\.js$/);\nmodules.keys().forEach(key => {\n  const mod = modules(key);\n  console.log('Loaded module:', mod);\n});\n\n// Then run with: node -r @babel/register entry.js","lang":"javascript","description":"Demonstrates setup: add plugin to .babelrc, register polyfill at entry point, then use require.context with three arguments."},"warnings":[{"fix":"Call require('babel-plugin-require-context-hook/register')() at the very top of your entry file, before requiring any module that uses require.context.","message":"Register function must be called before any require.context usage. If called after, the global polyfill is not set.","severity":"gotcha","affected_versions":">=1.0.0"},{"fix":"Always provide all three arguments: require.context(directory, useSubdirectories, regExp). If you don't care about a filter, use /^./.","message":"Missing third argument (regExp) in require.context call will cause a runtime error because the polyfill expects exactly three arguments.","severity":"gotcha","affected_versions":">=1.0.0"},{"fix":"Use the short plugin name: \"require-context-hook\" without the 'babel-plugin-' prefix.","message":"The 'babel-plugin-' prefix in .babelrc plugins list may cause double registration and break plugin detection.","severity":"breaking","affected_versions":"<=1.0.0"}],"env_vars":null,"last_verified":"2026-04-25T00:00:00.000Z","next_check":"2026-07-24T00:00:00.000Z","problems":[{"fix":"Add require('babel-plugin-require-context-hook/register')() at the very top of your entry file, before any other require calls.","cause":"The register script has not been called, or was called after modules using require.context were loaded.","error":"Error: require.context is not a function"},{"fix":"Provide a third argument to require.context, e.g., require.context('./', true, /\\.js$/).","cause":"The third argument (regExp) to require.context is missing; the polyfill regex conversion expects a regex string.","error":"TypeError: Cannot read property 'replace' of undefined"}],"ecosystem":"npm","meta_description":null,"install_score":null,"install_tag":null,"quickstart_score":null,"quickstart_tag":null}