{"id":16533703,"url":"https://github.com/jantimon/reproduction-webpack-import-module-bug","last_synced_at":"2025-10-08T22:12:51.345Z","repository":{"id":219736139,"uuid":"749775102","full_name":"jantimon/reproduction-webpack-import-module-bug","owner":"jantimon","description":"Reproduction of a webpack importModule loader bug","archived":false,"fork":false,"pushed_at":"2024-01-29T12:25:31.000Z","size":654,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-03-03T13:33:30.171Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/jantimon.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null}},"created_at":"2024-01-29T11:22:52.000Z","updated_at":"2024-01-29T11:24:14.000Z","dependencies_parsed_at":"2024-01-29T13:59:43.718Z","dependency_job_id":"6330e50c-8e93-4d67-8324-42a33c06adc1","html_url":"https://github.com/jantimon/reproduction-webpack-import-module-bug","commit_stats":null,"previous_names":["jantimon/reproduction-webpack-import-module-bug"],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/jantimon/reproduction-webpack-import-module-bug","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jantimon%2Freproduction-webpack-import-module-bug","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jantimon%2Freproduction-webpack-import-module-bug/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jantimon%2Freproduction-webpack-import-module-bug/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jantimon%2Freproduction-webpack-import-module-bug/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/jantimon","download_url":"https://codeload.github.com/jantimon/reproduction-webpack-import-module-bug/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jantimon%2Freproduction-webpack-import-module-bug/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":263165691,"owners_count":23423994,"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":[],"created_at":"2024-10-11T18:15:37.930Z","updated_at":"2025-10-08T22:12:46.305Z","avatar_url":"https://github.com/jantimon.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"## Reproduction:\n\n```\nnpm i\nnode server.mjs\n```\n\n[Reproduction for Next.js](https://github.com/jantimon/reproduction-webpack-import-module-bug/tree/nextjs)\n\n## Error:\n\n![error screenshot](https://github.com/jantimon/reproduction-webpack-import-module-bug/assets/4113649/17735e8e-5b63-4619-ba17-cb378ee73be8)\n\n\n## Reason:\n\n`./loader.cjs` uses the Webpack loader API `this.importModule` which invokes `vm.runInThisContext` to execute the module code.\n\nHowever, as soon as the `package.json` file includes `\"type\": \"module\"` `vm.runInThisContext` does not have access to `require` and `module` anymore.\n\n\n## Possible fix:\n\nAdd `require` and `module` to compilation.hooks.executeModule.tap(PLUGIN_NAME, (options, context) =\u003e { ... }):\n\n```diff\ndiff --git a/node_modules/webpack/lib/javascript/JavascriptModulesPlugin.js b/node_modules/webpack/lib/javascript/JavascriptModulesPlugin.js\nindex 4249a2f..31cd251 100644\n--- a/node_modules/webpack/lib/javascript/JavascriptModulesPlugin.js\n+++ b/node_modules/webpack/lib/javascript/JavascriptModulesPlugin.js\n@@ -441,8 +441,11 @@ class JavascriptModulesPlugin {\n \t\t\t\t\tconst { module, moduleObject } = options;\n \t\t\t\t\tconst code = source.source();\n \n+\t\t\t\t\tconst {createRequire} = require(\"module\");\n+\t\t\t\t\tconst universalRequire = typeof createRequire === \"function\" ? createRequire(module.resource) : require;\n+\n \t\t\t\t\tconst fn = vm.runInThisContext(\n-\t\t\t\t\t\t`(function(${module.moduleArgument}, ${module.exportsArgument}, ${RuntimeGlobals.require}) {\\n${code}\\n/**/})`,\n+\t\t\t\t\t\t`(function(${module.moduleArgument}, ${module.exportsArgument}, ${RuntimeGlobals.require}, require, module) {\\n${code}\\n/**/})`,\n \t\t\t\t\t\t{\n \t\t\t\t\t\t\tfilename: module.identifier(),\n \t\t\t\t\t\t\tlineOffset: -1\n@@ -453,7 +456,9 @@ class JavascriptModulesPlugin {\n \t\t\t\t\t\t\tmoduleObject.exports,\n \t\t\t\t\t\t\tmoduleObject,\n \t\t\t\t\t\t\tmoduleObject.exports,\n-\t\t\t\t\t\t\tcontext.__webpack_require__\n+\t\t\t\t\t\t\tcontext.__webpack_require__,\n+\t\t\t\t\t\t\tuniversalRequire,\n+\t\t\t\t\t\t\tmoduleObject\n \t\t\t\t\t\t);\n \t\t\t\t\t} catch (e) {\n \t\t\t\t\t\te.stack += printGeneratedCodeForStack(\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjantimon%2Freproduction-webpack-import-module-bug","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjantimon%2Freproduction-webpack-import-module-bug","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjantimon%2Freproduction-webpack-import-module-bug/lists"}