You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

README.md 3.7KB

4 years ago
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. [![NPM version][npm-image]][npm-url] [![Build Status][travis-image]][travis-url]
  2. [![Dependency Status][daviddm-image]][daviddm-url] [![devDependency Status][daviddm-dev-image]][daviddm-dev-url]
  3. # Optional Require
  4. NodeJS Require that let you handle module not found error without try/catch. Allows you to gracefully require a module only if it exists and contains no error.
  5. # Usage
  6. ```js
  7. const optionalRequire = require("optional-require")(require);
  8. const foo = optionalRequire("foo") || {};
  9. const bar = optionalRequire("bar", true); // true enables console.log a message when not found
  10. const xyz = optionalRequire("xyz", "test"); // "test" enables console.log a message with "test" added.
  11. const fbPath = optionalRequire.resolve("foo", "foo doesn't exist");
  12. const rel = optionalRequire("../foo/bar"); // relative module path works
  13. ```
  14. # Install
  15. ```bash
  16. $ npm i optional-require --save
  17. ```
  18. # API
  19. #### [optionalRequire(require)](#optionalrequirerequire)
  20. The single function this module exports. Call it with `require` to get a custom function for you to do optional require from your file's require context. See [Usage](#usage) above.
  21. #### [customOptionalRequire(path, \[message|options\])](#customoptionalrequirepath-messageoptions)
  22. The function [optionalRequire](#optionalrequirerequire) returns for you to do optional require from your file's require context.
  23. ##### Params
  24. - `path` - name/path to the module your want to optionally require
  25. - `message` - optional flag/message to enable `console.log` a message when module is not found
  26. - `options` - an optional object with the following fields
  27. - `message` - see above
  28. - `fail` - callback for when an error that's _not_ `MODULE_NOT_FOUND` for `path` occurred
  29. - `notFound` - callback for when `path` was not found
  30. - The value from this is returned
  31. - `default` - default value to returned when not found - not allowed with `notFound` together
  32. ##### Returns
  33. - module required or one of the following if not found
  34. - `undefined` or
  35. - return value from `options.notFound` if it's specified
  36. - `options.default` if it's specified
  37. ##### Throws
  38. - rethrows any error that's not `MODULE_NOT_FOUND` for the module `path`
  39. #### [customOptionalRequire.resolve(path, \[message\])](#customoptionalrequireresolvepath-message)
  40. Same as [customOptionalRequire](#customoptionalrequirepath-messageoptions) but acts like `require.resolve`
  41. #### [optionalRequire.log(message, path)](#optionalrequirelogmessage-path)
  42. The function that will be called to log the message when optional module is not found. You can override this with your own function.
  43. #### [optionalRequire.try(require, path, \[message|options\])](#optionalrequiretryrequire-path-messageoptions)
  44. Same as [customOptionalRequire](#customoptionalrequirepath-messageoptions) but you have to pass in `require` from your file's context.
  45. #### [optionalRequire.resolve(require, path, \[message|options\])](#optionalrequireresolverequire-path-messageoptions)
  46. Same as [customOptionalRequire.resolve](#customoptionalrequirepath-messageoptions) but you have to pass in `require` from your file's context.
  47. # LICENSE
  48. Apache-2.0 © [Joel Chen](https://github.com/jchip)
  49. [travis-image]: https://travis-ci.org/jchip/optional-require.svg?branch=master
  50. [travis-url]: https://travis-ci.org/jchip/optional-require
  51. [npm-image]: https://badge.fury.io/js/optional-require.svg
  52. [npm-url]: https://npmjs.org/package/optional-require
  53. [daviddm-image]: https://david-dm.org/jchip/optional-require/status.svg
  54. [daviddm-url]: https://david-dm.org/jchip/optional-require
  55. [daviddm-dev-image]: https://david-dm.org/jchip/optional-require/dev-status.svg
  56. [daviddm-dev-url]: https://david-dm.org/jchip/optional-require?type=dev