Coder Social home page Coder Social logo

Add `find` method to require about node HOT 10 CLOSED

nodejs avatar nodejs commented on April 28, 2024
Add `find` method to require

from node.

Comments (10)

missinglink avatar missinglink commented on April 28, 2024

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?

from node.

rumkin avatar rumkin commented on April 28, 2024

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

from node.

fampinheiro avatar fampinheiro commented on April 28, 2024

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

from node.

feross avatar feross commented on April 28, 2024

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

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

from node.

rumkin avatar rumkin commented on April 28, 2024

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

from node.

aredridel avatar aredridel commented on April 28, 2024

Why can't it?

from node.

rumkin avatar rumkin commented on April 28, 2024

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

from node.

aredridel avatar aredridel commented on April 28, 2024

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)

from node.

missinglink avatar missinglink commented on April 28, 2024

@rumkin, @aredridel please stick to the reported issue and avoid speculation.
https://github.com/iojs/io.js/blob/v0.12/CONTRIBUTING.md#issue-contributions

from node.

chrisdickinson avatar chrisdickinson commented on April 28, 2024

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.

from node.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.