Sitelet https://github.com/nodejs/node/issues/70
Skip to content

Add find method to require #70

Description

@rumkin

Actually it's very strange behaviour of require.resolve, which throws an error when package not found. Add require.find method which would return null if package not found.

Activity

  1. changed the title [-]Add find method to require[/-] [+]Add `find` method to require[/+] on Dec 4, 2014
  2. missinglink commented on Dec 4, 2014

    @missinglink
    Contributor

    Implementing this behaviour on your own is trivial:

    function find( moduleName ){
      try {
        return require.resolve( moduleName );
      } catch( e ){
        return null;
      }
    }

    What exactly is strange about it and why do you feel it is a wide enough issue that it deserves a new core API method?

  3. rumkin commented on Dec 4, 2014

    @rumkin
    ContributorAuthor

    @missinglink Yep, it's trivial. And platform API should be clean and trivial, isn't? But the problem is that require is context-dependent function (it works different in each file). So it means that I should to implement find method in each file. It's not rational. And finally if there is way to do something without throwing an Error it should be done so.

  4. fampinheiro commented on Dec 4, 2014

    @fampinheiro

    @rumkin you can use a module like optional if you don't want to wrap your requires.

  5. feross commented on Dec 4, 2014

    @feross
    Contributor

    -1. The module loading code is Stability: 5 - Locked and unlikely to ever change.

    Just use @missinglink's snippet or the optional module.

  6. rumkin commented on Dec 4, 2014

    @rumkin
    ContributorAuthor

    @fampinheiro Are you serious? Was you looking it's source? It will not work like it should because it not context-dependent and couldn't be.
    @feross It's for node API. iojs will change loading code because it cannot use node_modules folder forever.

  7. aredridel commented on Dec 4, 2014

    @aredridel
    Contributor

    Why can't it?

  8. rumkin commented on Dec 4, 2014

    @rumkin
    ContributorAuthor

    @aredridel Because iojs could not control Joyent policy so backward compatibility will be broken soon (after version 1.0). To prevent a mess of io and node packages module directory should be renamed.

  9. aredridel commented on Dec 4, 2014

    @aredridel
    Contributor

    Not sure why they'd be separate. node_modules is deeply baked in, and the module system is locked stability.

    and there's no guarantee that a breaking change will ever happen, nor would changing the folder name help that (it'd only break more)

  10. missinglink commented on Dec 4, 2014

    @missinglink
    Contributor
  11. chrisdickinson commented on Dec 4, 2014

    @chrisdickinson
    Contributor

    I don't think this will change in the near future:

    1. It would expand the surface area of a locked subsystem.
    2. This behavior can be approximated in userland (and there exist packages to make that easier!)

    Closing for now.

  12. added a commit that references this issue on Feb 26, 2017
    32b85f9
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