{"id":19451146,"url":"https://github.com/thekashey/require-control","last_synced_at":"2025-07-16T08:10:19.770Z","repository":{"id":57354215,"uuid":"133031135","full_name":"theKashey/require-control","owner":"theKashey","description":"Get the full control over the nodejs module system.","archived":false,"fork":false,"pushed_at":"2023-02-09T03:39:39.000Z","size":120,"stargazers_count":12,"open_issues_count":3,"forks_count":1,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-07-05T02:03:00.502Z","etag":null,"topics":["dependency-injection","mocking","module-system","nodejs"],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/theKashey.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2018-05-11T11:25:21.000Z","updated_at":"2023-08-23T20:14:48.000Z","dependencies_parsed_at":"2024-10-29T12:52:37.109Z","dependency_job_id":null,"html_url":"https://github.com/theKashey/require-control","commit_stats":{"total_commits":23,"total_committers":3,"mean_commits":7.666666666666667,"dds":0.08695652173913049,"last_synced_commit":"6529ac02b2f4cee81a3681f8dfb29e17c1a73c5a"},"previous_names":[],"tags_count":7,"template":false,"template_full_name":null,"purl":"pkg:github/theKashey/require-control","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/theKashey%2Frequire-control","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/theKashey%2Frequire-control/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/theKashey%2Frequire-control/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/theKashey%2Frequire-control/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/theKashey","download_url":"https://codeload.github.com/theKashey/require-control/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/theKashey%2Frequire-control/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":265495432,"owners_count":23776629,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["dependency-injection","mocking","module-system","nodejs"],"created_at":"2024-11-10T16:40:27.335Z","updated_at":"2025-07-16T08:10:19.733Z","avatar_url":"https://github.com/theKashey.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# require-control [![Build Status](https://secure.travis-ci.org/theKashey/require-control.svg)](http://travis-ci.org/theKashey/require-control) [![Greenkeeper badge](https://badges.greenkeeper.io/theKashey/require-control.svg)](https://greenkeeper.io/)\n\nThis is a module-in-the-middle, require hack, path control, this is just a toolbox to master nodejs modules.\n\n- Fast relief againts the imports inside your node_modules. \n- A cure from \"something.`default` is not a thing\".\n- A way to tame module name resolution.\n\n# API\n- All commands returns a function to cancel side effects.\n- All commands could be run as many times as you wish, but only name resolution will make sense.\n\n### 1. To cure all `imports` nodejs could look inside packages, stored out of sight of babel, inside node_modules\n```js\nimport 'require-control/register';\n```\nAnd then all `imports` inside node_modules will work\n\n### 2. To make your project _webpack-compact_(babel interop require) by forcing `.default` to exist on exports\n```js\nimport 'require-control/interop-require';\n```\nAnd then default will _always_ exists.\n\n### 3. To create a loop-aware module cache\n```js\nimport {mirrorModuleCache} from 'require-control';\nmirrorModuleCache((requireName, absoluteFilePath) =\u003e boolean);\n```\nAnd that will change how cache works for the selected files.\n\n### 4. To control path resolution\n```js\nimport {nameResolution} from 'require-control';\nnameResolution((requireName, parent) =\u003e newFileName);\n\nimport {setAliases} from 'require-control';\nsetAliases({\n  'from': 'to'\n});\n```\nAnd then all module aliases (webpack resolve, ts paths, aliases, anything)\n\nPS: You can use setAliases to achieve 10-100 speedup against tsconfig-paths, if you are not using \"complex\" typescript paths.\n\nPPS: `setAlias` is doing the same [module-alias](https://github.com/ilearnio/module-alias) does - changes how `Module._resolveFilename` works. If you want to migrate to `aliases` from `relative` imports you might use [relative-to-alias](https://github.com/s-yadav/relative-to-alias) or [restructor](https://github.com/theKashey/restructor).\n\n### 4.5 To control module resolution order\n```js\nimport {hoistExtensions} from 'require-control';\n\n// make tsx, ts and jsx be picked first\nhoistExtensions(['.tsx', '.ts', '.jsx']);\n``` \nThat could be quite usefull, if you have `Component.less` and `Component.jsx` or `Component.tsx`, as long as\n`require('./Component')` would load `.less` file - it goes before any other \"non-native\" extension.\n\nPS: This command just moves listed extensions prior non-listed.\n\n### 5. To apply any babel transformation, anywhere\n`babelIt(pick, babelSettings)`, where pick could return true, false, or code to pipe into babel.\n```js\nimport {babelIt} from 'require-control';\n\nbabelIt( fileName =\u003e fileName.includes('my-folder'), {\n  babelrc: false,\n  plugin: ['power-converter']\n})\n```\n\n## Examples\n\n#### TypeScript + ES6 + Mocha\n\n```js\nrequire('ts-node/register');  // support TS\nrequire('babel-register');    // support ES\n\nconst { esm_modules, interopRequire, setAliases, resolutions } = require('require-control');\n\nesm_modules();                // support ES in node_modules\ninteropRequire();             // \"webpack\" default imports everywhere in js,jsx,less,scss\ninteropRequire(['.less']);    // \"webpack\" default imports for .less files\n\nsetAliases({                  // why not!\n  'common': path.join(root, 'src/common'),\n  'components': path.join(root, 'src/components'),\n});\n\nresolutions(request =\u003e {      // custom resolution\n  if (request === 'react') {\n    return 'preact';\n  }\n});\n```\n\n#### Webpack aliases (or typescript paths)\n```js\nconst {setAliases } = require('require-control');\n\nsetAliases({                  \n  'common': path.join(root, 'src/common'),\n  'components': path.join(root, 'src/components'),\n});\n```\n\n### Would not be done without\nThis project would not be ever created without my work on [rewiremock](https://github.com/theKashey/rewiremock).\n\n# Licence\nMIT\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fthekashey%2Frequire-control","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fthekashey%2Frequire-control","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fthekashey%2Frequire-control/lists"}