Repository navigation
import(cjs) with query strings has odd behavior #29812
Description
Activity
- addedesmIssues and PRs related to the ECMAScript Modules implementation.Issues and PRs related to the ECMAScript Modules implementation.
on Oct 3, 2019 This issue is still valid in Node.js 13.0.1.
Here is how CJS modules are loaded into the ESM loader:
- Whenever a CJS module is loaded via
require(), it is injected at its path into the ESM loader (without any query string) - Whenever an CJS module is loaded from
import, it calls out to the CJS loader, which then injects the module value back.
As a result: CommonJS modules loaded with query strings will not load their exports, AND query strings will not reload the module.
This is the way the design works unfortunately and it cannot really be changed. It was done this way because it was deemed important to "snapshot" CommonJS modules at their time of execution to inject them into the ESM loader.
The only way to allow the model this issue is asking for would be to remove that "snapshot" requirement, which I'm not sure is possible.
That said if it is possible - now would be the time to argue for it.
- Whenever a CJS module is loaded via
- addedfeature requestIssues requesting new Node.js features.Issues requesting new Node.js features.
on Mar 12, 2020 Should this be closed as
known limitationnow that #35781 has landed?There has been no activity on this feature request for 5 months and it is unlikely to be implemented. It will be closed 6 months after the last non-automated comment.
For more information on how the project manages feature requests, please consult the feature request management document.
- addedstaleIssues and PRs marked stale due to inactivity and scheduled for automatic closure.Issues and PRs marked stale due to inactivity and scheduled for automatic closure.
on Mar 24, 2022 There has been no activity on this feature request and it is being closed. If you feel closing this issue is not the right thing to do, please leave a comment.
For more information on how the project manages feature requests, please consult the feature request management document.
From eslint/eslint#12333
Repro https://github.com/mysticatea/import-cjs-issue
Description
I tried to import packages without import cache. From the document, it looks I should use query strings.
x-esmis an ES module package. It worked as expected.x-cjsis a CJS package. The result was odd. Theconsole.log()inx-cjspackage ran only one time, but the returned values are different for each query string.I found the entry of
x-cjsinrequire.cache. However, the cache entry is odd as well. It's different fromrequire("x-cjs"), the entry doesn't haveparentproperty and themodule.childrenoftest.jsis still empty.Anyway, I tried to remove the cache entry.
Cryptic. I guess this behavior is:
import(cjs)has cache apart fromrequire.cache.import(cjs)cache is created fromrequire.cache.require.cacheentry was not found.import(cjs)cache is not removed even ifrequire.cacheentry deleted.Therefore, I have to do the following steps if I want to import packages without cache.
require.cacheentry.Questions
import(cjs)creates incompleterequire.cacheentries?import(cjs)with query strings returns different objects for the same CJS package?I'm guessing that
import(cjs)should not create anyrequire.cacheentries, andimport(cjs)with query strings re-runs CJS packages as same as ES packages.