You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Differentiate between core modules and userland modules #562
TMK there are a few modules on NPM that have a name clash with core modules. To a large extent I think NPM has done the work of preventing this from happening, but I believe there is at least one or two core modules that name clash with NPM modules.
My guess is that if X is both a core module and NPM module, that the NPM module will get precedence given the way require works in Node.js?
My question is, assuming there is a name clash:
How can I require the node core module with 100% assurance?
How can I require the NPM module with 100% assurance?
Unfortunately, I cannot remember the name of the core module clash, but I swear there is at least one if not several.
Well you said: "Honesty compels me to admit that the npm module came first." That statement totally confused me, making me think that the userland module came first :)
No npm module should have the same name as a node core module. No node core module should be created that shadows the name of an npm module, either, but that's ancient history by now. It seems to me that 'cluster' is abandoned: https://github.com/LearnBoost/cluster/commits/master, if it was still active, I suspect they would publish it under a new name.
@sam-github yeah I didn't realize that 'cluster' was the only name-clash, I thought there were others. In any case, like I said, if there were ever a name clash again, as it stands now, you can just add a slash and the NPM module would be required.
require('cluster/');
although that's getting into some messier territory.
It's not the only clash, there are some others, but it doesn't really matter because unless you try and explicitly load it, the built-ins will be loaded first.
Activity
bnoordhuis commentedon Apr 4, 2017
No, it's special-cased. Built-in modules always take precedence.
cluster? Honesty compels me to admit that the npm module came first.
the1mills commentedon Apr 4, 2017
Ok :) thanks is there any good solution to accurately sourcing a core module instead of the NPM module?
Process.binding(X) or something?
ORESoftware commentedon Apr 4, 2017
@bnoordhuis actually, according to my testing, the core module loaded first
I just have this script:
then I simply modified the index.js file in node_modules/cluster/index.js like so:
didn't get the log statement, so it appears core module loaded first
I will ask the cluster module folks about what's going on and report back...
ORESoftware commentedon Apr 4, 2017
@bnoordhuis just tested again, both devDependency or regular dependency, core module gets loaded first for me, I am on node.js version 6.9.5
bnoordhuis commentedon Apr 5, 2017
Yes, that's what I said: built-in modules always take precedence.
ORESoftware commentedon Apr 5, 2017
Well you said: "Honesty compels me to admit that the npm module came first." That statement totally confused me, making me think that the userland module came first :)
ORESoftware commentedon Apr 5, 2017
@bnoordhuis my guess is that you can require the cluster userland module with
require('cluster/')but that's just a guess, I would have to test.ORESoftware commentedon Apr 5, 2017
yep that worked, just FYI, crazytown
SomeoneWeird commentedon Apr 5, 2017
@bnoordhuis means that the npm package was published first, but then a built-in module was created with the same name.
So even though the npm module was created first, it won't be loaded if you
require("cluster")sam-github commentedon Apr 6, 2017
No npm module should have the same name as a node core module. No node core module should be created that shadows the name of an npm module, either, but that's ancient history by now. It seems to me that 'cluster' is abandoned: https://github.com/LearnBoost/cluster/commits/master, if it was still active, I suspect they would publish it under a new name.
ORESoftware commentedon Apr 6, 2017
@sam-github yeah I didn't realize that 'cluster' was the only name-clash, I thought there were others. In any case, like I said, if there were ever a name clash again, as it stands now, you can just add a slash and the NPM module would be required.
although that's getting into some messier territory.
SomeoneWeird commentedon Apr 6, 2017
It's not the only clash, there are some others, but it doesn't really matter because unless you try and explicitly load it, the built-ins will be loaded first.