{"id":19504048,"url":"https://github.com/mysticatea/import-cjs-issue","last_synced_at":"2025-02-25T21:45:14.226Z","repository":{"id":65974394,"uuid":"212269486","full_name":"mysticatea/import-cjs-issue","owner":"mysticatea","description":null,"archived":false,"fork":false,"pushed_at":"2019-10-02T07:48:10.000Z","size":4,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-01-08T10:40:38.847Z","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":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/mysticatea.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}},"created_at":"2019-10-02T06:24:38.000Z","updated_at":"2019-10-02T07:48:12.000Z","dependencies_parsed_at":"2023-02-19T17:45:17.301Z","dependency_job_id":null,"html_url":"https://github.com/mysticatea/import-cjs-issue","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mysticatea%2Fimport-cjs-issue","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mysticatea%2Fimport-cjs-issue/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mysticatea%2Fimport-cjs-issue/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mysticatea%2Fimport-cjs-issue/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/mysticatea","download_url":"https://codeload.github.com/mysticatea/import-cjs-issue/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":240754294,"owners_count":19852186,"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-11-10T22:24:06.326Z","updated_at":"2025-02-25T21:45:14.195Z","avatar_url":"https://github.com/mysticatea.png","language":"JavaScript","readme":"# `import(cjs)` issue\n\nWith Node.js v12.11.1, On Windows 10,\n\n## test.js\n\n```\nnode --experimental-modules test.js\n```\n\n\u003cdetails\u003e\u003csummary\u003eSource Code\u003c/summary\u003e\n\n```js\nconsole.log(\"version:\", process.version)\n\n;(async () =\u003e {\n    console.log(\"==== ESM ====\")\n    await import(\"x-esm\") //→ Found as expected\n    await import(\"./node_modules/x-esm/index.js?q=0\") //→ Found and `x-esm` re-ran as expected\n    await import(\"./node_modules/x-esm/index.js?q=1\") //→ Found and `x-esm` re-ran as expected\n\n    console.log(\"==== CJS ====\")\n    const cjs = await import(\"x-cjs\") //→ Found as expected\n    const cjs0 = await import(\"./node_modules/x-cjs/index.js?q=0\") //→ Found but `x-cjs` didn't re-run\n    const cjs1 = await import(\"./node_modules/x-cjs/index.js?q=1\") //→ Found but `x-cjs` didn't re-run\n    console.log(cjs === cjs0, cjs === cjs1, cjs0 === cjs1) //→ all are false but `x-cjs` didn't run three times\n    \n    console.log(\"---- remove 'require.cache' ----\")\n    delete require.cache[require.resolve(\"x-cjs\")]\n    await import(\"x-cjs\") //→ Found but `x-cjs` didn't re-run\n    await import(\"./node_modules/x-cjs/index.js?q=0\") //→ Found but `x-cjs` didn't re-run\n    await import(\"./node_modules/x-cjs/index.js?q=1\") //→ Found but `x-cjs` didn't re-run\n    await import(\"./node_modules/x-cjs/index.js?q=2\") //→ Found and `x-cjs` re-ran as expected\n    await import(\"./node_modules/x-cjs/index.js?q=3\") //→ Found but `x-cjs` didn't re-run\n})().catch(error =\u003e {\n    console.error(error)\n    process.exitCode = 1\n})\n```\n\n\u003c/details\u003e\n\n\u003cdetails\u003e\u003csummary\u003eResult\u003c/summary\u003e\n\n```\n(node:14972) ExperimentalWarning: The ESM module loader is experimental.\nversion: v12.11.1\n==== ESM ====\nx-esm\nx-esm\nx-esm\n==== CJS ====\nx-cjs\nfalse false false\n---- remove 'require.cache' ----\nx-cjs\n```\n\n\u003c/details\u003e\n\n### Description\n\nI tried to import packages without import cache. From the document, it looks I should use query strings.\n\n```js\n    await import(\"x-esm\") //→ Found as expected\n    await import(\"./node_modules/x-esm/index.js?q=0\") //→ Found and `x-esm` re-ran as expected\n    await import(\"./node_modules/x-esm/index.js?q=1\") //→ Found and `x-esm` re-ran as expected\n```\n\n`x-esm` is an ES module package. It worked as expected.\n\n```js\n    const cjs = await import(\"x-cjs\") //→ Found as expected\n    const cjs0 = await import(\"./node_modules/x-cjs/index.js?q=0\") //→ Found but `x-cjs` didn't re-run\n    const cjs1 = await import(\"./node_modules/x-cjs/index.js?q=1\") //→ Found but `x-cjs` didn't re-run\n    console.log(cjs === cjs0, cjs === cjs1, cjs0 === cjs1) //→ all are false but `x-cjs` didn't run three times\n```\n\n`x-cjs` is a CJS package. The result was odd. The `console.log()` in `x-cjs` package ran only one time, but the returned values are different for each query string.\n\nI found the entry of `x-cjs` in `require.cache`. However, the cache entry is odd as well. It's different from `require(\"x-cjs\")`, the entry doesn't have `parent` property and the `module.children` of `test.js` is still empty.\n\nAnyway, I tried to remove the cache entry.\n\n```js\n    console.log(\"---- remove 'require.cache' ----\")\n    delete require.cache[require.resolve(\"x-cjs\")]\n    await import(\"x-cjs\") //→ Found but `x-cjs` didn't re-run\n    await import(\"./node_modules/x-cjs/index.js?q=0\") //→ Found but `x-cjs` didn't re-run\n    await import(\"./node_modules/x-cjs/index.js?q=1\") //→ Found but `x-cjs` didn't re-run\n    await import(\"./node_modules/x-cjs/index.js?q=2\") //→ Found and `x-cjs` re-ran as expected\n    await import(\"./node_modules/x-cjs/index.js?q=3\") //→ Found but `x-cjs` didn't re-run\n```\n\nCryptic. I guess this behavior is:\n\n- `import(cjs)` has cache apart from `require.cache`.\n- The `import(cjs)` cache is created from `require.cache`.\n- It runs CJS package only if `require.cache` entry was not found.\n- The `import(cjs)` cache is not removed even if `require.cache` entry deleted.\n\nTherefore, I have to do the following steps if I want to import packages without cache.\n\n1. Find the main file of the package because I cannot put query strings to the package name.\n   ```js\n   const url = \"file:\" + require.resolve(packageName) + uniqueQueryString;\n   ```\n1. Import it.\n   ```js\n   const ret = await import(url);\n   ```\n1. Delete `require.cache` entry.\n   ```js\n   delete require.cache[require.resolve(packageName)];\n   ```\n\n### Questions\n\n1. Is it intentional behavior that `import(cjs)` creates incomplete `require.cache` entries?\n1. If yes, is it intentional behavior that `import(cjs)` with query strings returns different objects for the same CJS package?\n\nI'm guessing that `import(cjs)` should not create any `require.cache` entries, and `import(cjs)` with query strings re-runs CJS packages as same as ES packages.\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmysticatea%2Fimport-cjs-issue","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmysticatea%2Fimport-cjs-issue","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmysticatea%2Fimport-cjs-issue/lists"}