{"id":19156,"library":"babel-plugin-require-context-hook","title":"Babel Plugin Require Context Hook","description":"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.","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","lang":"bash","label":"npm"},{"cmd":"yarn add babel-plugin-require-context-hook","lang":"bash","label":"yarn"},{"cmd":"pnpm add babel-plugin-require-context-hook","lang":"bash","label":"pnpm"}],"dependencies":[],"imports":[{"note":"The register module exports a function that must be called immediately; it is not a default export.","wrong":"import { register } from 'babel-plugin-require-context-hook/register'","symbol":"register","correct":"require('babel-plugin-require-context-hook/register')();"},{"note":"The plugin is a Babel plugin, not a direct JavaScript import. Add it to your Babel configuration.","wrong":"require('babel-plugin-require-context-hook').default","symbol":"plugin","correct":"// in .babelrc: \"plugins\": [\"require-context-hook\"]"},{"note":"require.context is a Webpack feature; this plugin makes it available in Node by rewriting calls.","wrong":"import { context } from 'babel-plugin-require-context-hook'","symbol":"require.context","correct":"const modules = require.context('./components', true, /\\.js$/);"}],"quickstart":{"code":"// Install: npm install --save-dev babel-plugin-require-context-hook\n// .babelrc\n{\n  \"plugins\": [\"require-context-hook\"]\n}\n\n// In your entry or test setup file (e.g., setup.js)\nrequire('babel-plugin-require-context-hook/register')();\n\n// Then use require.context() anywhere, e.g., in a Mocha test\nconst modules = require.context('./fixtures', true, /\\.json$/);\nmodules.keys().forEach(key => {\n  const data = modules(key);\n  console.log(data);\n});","lang":"javascript","description":"Shows installation, Babel configuration, registering the global helper, and using require.context() in a Node script."},"warnings":[{"fix":"Ensure require('babel-plugin-require-context-hook/register')() is executed at the top of your entry point or in a setup file.","message":"Requires calling the register function before any require.context usage; otherwise require.context is undefined.","severity":"gotcha","affected_versions":"all"},{"fix":"Use Webpack's native require.context for browser code.","message":"The global require.context function is not available in the browser; only works in Node with babel-register.","severity":"breaking","affected_versions":"all"},{"fix":"","message":"No known deprecations.","severity":"deprecated","affected_versions":">=0.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 top of your entry file.","cause":"The register script was not called before trying to use require.context.","error":"require.context is not a function"},{"fix":"Check .babelrc includes 'require-context-hook' in plugins, and the register function is called.","cause":"The Babel plugin is not configured or the register step is missing.","error":"TypeError: Cannot read property 'keys' of undefined"},{"fix":"Run npm install --save-dev babel-plugin-require-context-hook","cause":"The package is not installed in devDependencies.","error":"Module not found: Error: Cannot resolve module 'babel-plugin-require-context-hook'"}],"ecosystem":"npm","meta_description":null,"install_score":null,"install_tag":null,"quickstart_score":null,"quickstart_tag":null}