Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Differentiate between core modules and userland modules #562

Closed
ORESoftware opened this issue Apr 4, 2017 · 12 comments
Closed

Differentiate between core modules and userland modules #562

ORESoftware opened this issue Apr 4, 2017 · 12 comments

Comments

@ORESoftware
Copy link

ORESoftware commented Apr 4, 2017

This regards Node versions 4.0.0 <--> latest

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:

  1. How can I require the node core module with 100% assurance?
  2. 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.

@bnoordhuis
Copy link
Member

that the NPM module will get precedence given the way require works in Node.js?

No, it's special-cased. Built-in modules always take precedence.

I cannot remember the name of the core module clash

cluster? Honesty compels me to admit that the npm module came first.

@the1mills
Copy link

the1mills commented 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
Copy link
Author

@bnoordhuis actually, according to my testing, the core module loaded first

I just have this script:

const cluster = require('cluster');

then I simply modified the index.js file in node_modules/cluster/index.js like so:

screenshot 2017-04-04 15 46 02

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
Copy link
Author

ORESoftware commented 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
Copy link
Member

Yes, that's what I said: built-in modules always take precedence.

@ORESoftware
Copy link
Author

ORESoftware commented 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
Copy link
Author

@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
Copy link
Author

yep that worked, just FYI, crazytown

@SomeoneWeird
Copy link
Member

@bnoordhuis means that the npm package was published first, but then a built-in module was created with the same name.

No, it's special-cased. Built-in modules always take precedence.

So even though the npm module was created first, it won't be loaded if you require("cluster")

@sam-github
Copy link

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
Copy link
Author

@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.

@SomeoneWeird
Copy link
Member

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants