Skip to content

Why does module.js directly use the process bindings instead of fs? #4190

Description

@ariporad

Hi Node Core Team!

First of all, Thanks so much for making such an awesome thing!

Second, I'm currently working on developing something to make hooking require easier, it's called pirates. (I think very strongly that the node core should have an official way of hooking require, as it's a rather common thing to do, and very frequently done wrong, and maybe even worth bending the rules for 😉, but I know the module system is locked and off limits, so...).

As of right now, I'm trying to improve the tests (see danez/pirates#5). I wanted to do this with mock-fs, which overrides some methods of the fs module to be backed by a memory-based file system.

However, I discovered that module.js uses the fs binding directly to detect if file exist, despite using fs to actually read the file. I'm sure there's a good reason behind this that I'm just not smart enough to figure out, but for learning purposes, what is it? If there's no reason, would anyone object to me switching it (assuming the tests pass, of course)?

Thanks!

Activity

  1. added
    questionIssues asking questions about Node.js.
    moduleIssues and PRs related to the module subsystem.
    on Dec 8, 2015
  2. bnoordhuis commented on Dec 8, 2015

    @bnoordhuis
    Member

    The reason is performance, see commits 98649fd...1bbf8d0 and a71ee93.

  3. ariporad commented on Dec 8, 2015

    @ariporad
    Author

    Ahh... I afraid of that.

    This leads to the interesting situation that a non-existant module can't be mocked, but an existing one easily can. It would be nice if there was a way around this, any ideas? Currently, I have a directory of empty files for the purpose of mocking them out 😢.

  4. bnoordhuis commented on Dec 8, 2015

    @bnoordhuis
    Member

    I don't think there's an easy way around that. Maybe module.Module#_compile(code, filename)?

  5. MauriceButler commented on Dec 9, 2015

    @MauriceButler
    Contributor

    @ariporad Forgive me if I am misinterpreting your intent but a combination of the functionality
    here rooty and here fraudster might give you what you are looking for

    i.e. overriding the Module._load and Module._resolveFilename methods to inject your pipeline.

  6. ariporad commented on Dec 10, 2015

    @ariporad
    Author

    @MauriceButle: Unfortunately neither of those work (rooty looks useful though), I ended up with a directory of empty js files to mock out. Thanks though!

  7. bnoordhuis commented on Dec 10, 2015

    @bnoordhuis
    Member

    @ariporad Can we close this?

  8. ariporad commented on Dec 10, 2015

    @ariporad
    Author

    @bnoordhuis: Oh yeah, I forgot. Sorry.

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

    moduleIssues and PRs related to the module subsystem.questionIssues asking questions about Node.js.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions