Replies: 22 comments 10 replies
-
@nodejs/loaders |
Beta Was this translation helpful? Give feedback.
-
If you populate |
Beta Was this translation helpful? Give feedback.
-
@aduh95 why is that allowed for preferentially loaded modules ? |
Beta Was this translation helpful? Give feedback.
-
I don’t understand the question, what is allowed? |
Beta Was this translation helpful? Give feedback.
-
As your example demonstrates, manually modifying the cache entry for preferentially loaded core modules bypasses the intended preferential loading characteristics. This seems counterproductive to the purpose of preferential loading. |
Beta Was this translation helpful? Give feedback.
-
IIUC if you have a dependency named |
Beta Was this translation helpful? Give feedback.
-
I don’t know the reason, and it doesn’t really matter, that behavior cannot be changed without breaking the ecosystem. If you don’t like that behavior, you can add the |
Beta Was this translation helpful? Give feedback.
-
IIUC, the goal of preferential loading is to 'protect' core modules from being overwritten. Allowing manual manipulation of their cache entries seems to defeat this purpose. |
Beta Was this translation helpful? Give feedback.
-
Is manually manipulating cache entries for core modules a common scenario? |
Beta Was this translation helpful? Give feedback.
-
Like I said, the “goal” does not really matter, it is the way it is, and changing it is simply not worth it at this point. As someone who wasn’t involved at all in the development of CJS, I can’t tell you what was the rationale behind this design choice – or maybe it was an oversight, again, I have no idea – but as a maintainer I can tell you that whatever it is, it won’t change the fact that we want maximum stability on that area of the code base, and any PR that would try to amend the behavior would likely get rejected. |
Beta Was this translation helpful? Give feedback.
-
Then, at the very least, I suggest amending the documentation to clarify the behavior, with something like: "Some core modules are always preferentially loaded if their identifier is passed to require(). For instance, require('http') will return the built-in HTTP module, even if there is a file by that name. However, note that core modules imported without the node: prefix are served from the cache if an entry for the module exists. Since cache entries of core modules can be manually manipulated, this allows replacing the implementation for the dependent module. If you want to ensure obtaining the original implementation, use the node: prefix." |
Beta Was this translation helpful? Give feedback.
-
I’m not sure “the original implementation” is the correct phrasing, as it’s still possible to overwrite built-in modules as well (e.g. you could have |
Beta Was this translation helpful? Give feedback.
-
read: "If you want to ensure obtaining the original implementation" Alternatively: "If you want to ensure I'm fine with either. What do you prefer?
Sure. I can give it a try :) |
Beta Was this translation helpful? Give feedback.
-
However, since core modules are also "imported" I think "obtaining" is better. |
Beta Was this translation helpful? Give feedback.
-
If you use |
Beta Was this translation helpful? Give feedback.
-
Aren't core modules considered CommonJS modules? Isn't the CommonJS cache still accessible from an ES module? |
Beta Was this translation helpful? Give feedback.
-
No they considered… built-in modules. You can certainly access the require cache from ESM, but that has no effect on the matter. |
Beta Was this translation helpful? Give feedback.
-
IMO this should be converted to a discussion, am I okay to do that? |
Beta Was this translation helpful? Give feedback.
-
I don't understand. Are you saying that the require cache can't be manipulated in an ES module in the same way shown in the code you posted? |
Beta Was this translation helpful? Give feedback.
-
@redyetidev I have no objection. |
Beta Was this translation helpful? Give feedback.
-
Does an ES module importing a core module interact with an 'ES module cache' that isn't accessible to user modifications? |
Beta Was this translation helpful? Give feedback.
-
Affected URL(s)
https://nodejs.org/docs/latest-v20.x/api/modules.html#core-modules
Description of the problem
According to the documentation:
And:
Given this, if require('http') always returns the built-in HTTP module, under what circumstances would a require.cache entry by that name exist, making it necessary to use require('node:http')?
Beta Was this translation helpful? Give feedback.
All reactions