tallesl / node-safe-require

safe version of require that returns undefined rather than throwing an error when the module is not found

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

safe-require

A function that acts pretty much like require but returns undefined when the module is not found (instead of throwing an exception).

Usage

$ npm install safe-require
(...)
$ node
> var safeRequire = require('safe-require')
undefined
> safeRequire('url') // loads any module, local or not, just as require()
{ parse: [Function: urlParse],
  resolve: [Function: urlResolve],
  resolveObject: [Function: urlResolveObject],
  format: [Function: urlFormat],
  Url: [Function: Url] }
> safeRequire('nonexistent') // require('nonexistent') would throw "Cannot find module 'nonexistent'"
undefined

About

safe version of require that returns undefined rather than throwing an error when the module is not found

License:Other


Languages

Language:JavaScript 100.0%