{"id":15010488,"url":"https://github.com/nodejs/import-in-the-middle","last_synced_at":"2026-01-17T00:44:14.288Z","repository":{"id":43673196,"uuid":"395073138","full_name":"nodejs/import-in-the-middle","owner":"nodejs","description":"Like `require-in-the-middle`, but for ESM import","archived":false,"fork":false,"pushed_at":"2026-01-14T15:33:48.000Z","size":283,"stargazers_count":165,"open_issues_count":11,"forks_count":52,"subscribers_count":13,"default_branch":"main","last_synced_at":"2026-01-15T22:44:55.495Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"https://www.npmjs.com/package/import-in-the-middle","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/nodejs.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":"CONTRIBUTING.md","funding":null,"license":"LICENSE","code_of_conduct":"CODE_OF_CONDUCT.md","threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":"GOVERNANCE.md","roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null,"notice":"NOTICE","maintainers":null,"copyright":null,"agents":null,"dco":null,"cla":null},"funding":{"github":"nodejs","open_collective":"nodejs"}},"created_at":"2021-08-11T17:49:22.000Z","updated_at":"2026-01-15T20:57:48.000Z","dependencies_parsed_at":"2023-11-07T06:27:22.094Z","dependency_job_id":"1fd6d677-0514-44da-bbf8-167ae824eaa1","html_url":"https://github.com/nodejs/import-in-the-middle","commit_stats":{"total_commits":127,"total_committers":20,"mean_commits":6.35,"dds":0.6220472440944882,"last_synced_commit":"6dff6c5bc6acd272b2b9f875e640475fa97d7997"},"previous_names":["nodejs/import-in-the-middle"],"tags_count":49,"template":false,"template_full_name":null,"purl":"pkg:github/nodejs/import-in-the-middle","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nodejs%2Fimport-in-the-middle","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nodejs%2Fimport-in-the-middle/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nodejs%2Fimport-in-the-middle/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nodejs%2Fimport-in-the-middle/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/nodejs","download_url":"https://codeload.github.com/nodejs/import-in-the-middle/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nodejs%2Fimport-in-the-middle/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":28490523,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-01-16T23:55:29.509Z","status":"ssl_error","status_checked_at":"2026-01-16T23:55:29.108Z","response_time":107,"last_error":"SSL_connect returned=1 errno=0 peeraddr=140.82.121.5:443 state=error: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"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":[],"created_at":"2024-09-24T19:34:29.528Z","updated_at":"2026-01-17T00:44:14.262Z","avatar_url":"https://github.com/nodejs.png","language":"JavaScript","readme":"# import-in-the-middle\n\n**`import-in-the-middle`** is a module loading interceptor inspired by\n[`require-in-the-middle`](https://npm.im/require-in-the-middle), but\nspecifically for ESM modules. In fact, it can even modify modules after loading\ntime.\n\n## Usage\n\nThe API for\n`require-in-the-middle` is followed as closely as possible as the default\nexport. There are lower-level `addHook` and `removeHook` exports available which\ndon't do any filtering of modules, and present the full file URL as a parameter\nto the hook. See the Typescript definition file for detailed API docs.\n\nYou can modify anything exported from any given ESM or CJS module that's\nimported in ESM files, regardless of whether they're imported statically or\ndynamically.\n\n```js\nimport { Hook } from 'import-in-the-middle'\nimport { foo } from 'package-i-want-to-modify'\n\nconsole.log(foo) // whatever that module exported\n\nHook(['package-i-want-to-modify'], (exported, name, baseDir) =\u003e {\n  // `exported` is effectively `import * as exported from ${url}`\n  exported.foo += 1\n})\n\nconsole.log(foo) // 1 more than whatever that module exported\n```\n\nThis requires the use of an ESM loader hook, which can be added with the following\ncommand-line option.\n\n```shell\nnode --loader=import-in-the-middle/hook.mjs my-app.mjs\n```\n\nSince `--loader` has been deprecated you can also register the loader hook programmatically via the Node\n[`module.register()`](https://nodejs.org/api/module.html#moduleregisterspecifier-parenturl-options)\nAPI. However, for this to be able to hook non-dynamic imports, it needs to be\nregistered before your app code is evaluated via the `--import` command-line option.\n\n`my-loader.mjs`\n```js\nimport * as module from 'module'\n\nmodule.register('import-in-the-middle/hook.mjs', import.meta.url)\n```\n```shell\nnode --import=./my-loader.mjs ./my-code.mjs\n```\n\nWhen registering the loader hook programmatically, it's possible to pass a list\nof modules, file URLs or regular expressions to either `exclude` or specifically\n`include` which modules are intercepted. This is useful if a module is not\ncompatible with the loader hook. \n\n\u003e **Note:** This feature is incompatible with the `{internals: true}` Hook option\n\n```js\nimport * as module from 'module'\n\n// Exclude intercepting a specific module by name\nmodule.register('import-in-the-middle/hook.mjs', import.meta.url, {\n  data: { exclude: ['package-i-want-to-exclude'] }\n})\n\n// Only intercept a specific module by name\nmodule.register('import-in-the-middle/hook.mjs', import.meta.url, {\n  data: { include: ['package-i-want-to-include'] }\n})\n```\n\n### Only Intercepting Hooked modules \n\u003e **Note:** This feature is experimental and is incompatible with the `{internals: true}` Hook option\n\nIf you are `Hook`'ing all modules before they are imported, for example in a\nmodule loaded via the Node.js `--import` CLI argument, you can configure the\nloader to intercept only modules that were specifically hooked.\n\n`instrument.mjs`\n```js\nimport { register } from 'module'\nimport { Hook, createAddHookMessageChannel } from 'import-in-the-middle'\n\nconst { registerOptions, waitForAllMessagesAcknowledged } = createAddHookMessageChannel()\n\nregister('import-in-the-middle/hook.mjs', import.meta.url, registerOptions)\n\nHook(['fs'], (exported, name, baseDir) =\u003e {\n  // Instrument the fs module\n})\n\n// Ensure that the loader has acknowledged all the modules \n// before we allow execution to continue\nawait waitForAllMessagesAcknowledged()\n```\n`my-app.mjs`\n```js\nimport * as fs from 'fs'\n// fs will be instrumented!\nfs.readFileSync('file.txt')\n```\n\n```shell\nnode --import=./instrument.mjs ./my-app.mjs\n```\n\n### Experimental `experimentalPatchInternals` option\n\nIt was found that `import-in-the-middle` didn't match the hooking behavior of `require-in-the-middle` in some cases: \nhttps://github.com/nodejs/import-in-the-middle/issues/185\n\nThe `experimentalPatchInternals` option forces the loader to match the behavior of `require-in-the-middle` in these cases.\n\nThis option is experimental and may be removed or made the default in the future.\n\n```js\nimport { register } from 'module'\n\nregister('import-in-the-middle/hook.mjs', import.meta.url, {\n  data: { experimentalPatchInternals: true }\n})\n```\n\n## Limitations\n\n* You cannot add new exports to a module. You can only modify existing ones.\n* While bindings to module exports end up being \"re-bound\" when modified in a\n  hook, dynamically imported modules cannot be altered after they're loaded.\n* Modules loaded via `require` are not affected at all.\n","funding_links":["https://github.com/sponsors/nodejs","https://opencollective.com/nodejs"],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnodejs%2Fimport-in-the-middle","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fnodejs%2Fimport-in-the-middle","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnodejs%2Fimport-in-the-middle/lists"}