Skip to content

Differentiate between core modules and userland modules #562

Closed

Description

@ORESoftware

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.

Activity

  1. bnoordhuis commented on Apr 4, 2017

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

  2. the1mills commented on Apr 4, 2017

    @the1mills

    Ok :) thanks is there any good solution to accurately sourcing a core module instead of the NPM module?

    Process.binding(X) or something?

  3. ORESoftware commented on Apr 4, 2017

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

  4. ORESoftware commented on Apr 4, 2017

    @ORESoftware
    Author

    @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

  5. bnoordhuis commented on Apr 5, 2017

    @bnoordhuis
    Member

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

  6. ORESoftware commented on Apr 5, 2017

    @ORESoftware
    Author

    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 :)

  7. ORESoftware commented on Apr 5, 2017

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

  8. ORESoftware commented on Apr 5, 2017

    @ORESoftware
    Author

    yep that worked, just FYI, crazytown

  9. SomeoneWeird commented on Apr 5, 2017

    @SomeoneWeird
    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")

  10. sam-github commented on Apr 6, 2017

    @sam-github

    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.

  11. ORESoftware commented on Apr 6, 2017

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

  12. SomeoneWeird commented on Apr 6, 2017

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions