{"id":16682613,"url":"https://github.com/jchip/optional-require","last_synced_at":"2025-03-17T00:32:43.594Z","repository":{"id":46866141,"uuid":"80909012","full_name":"jchip/optional-require","owner":"jchip","description":"NodeJS Require that let you handle module not found error without try/catch","archived":false,"fork":false,"pushed_at":"2023-09-23T14:06:36.000Z","size":160,"stargazers_count":9,"open_issues_count":3,"forks_count":5,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-02-27T15:28:49.096Z","etag":null,"topics":["nodejs","optional","require"],"latest_commit_sha":null,"homepage":"","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/jchip.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2017-02-04T09:28:27.000Z","updated_at":"2024-07-04T13:11:14.000Z","dependencies_parsed_at":"2024-06-18T15:25:36.739Z","dependency_job_id":"dfc5643b-2da4-4380-8647-9b7798d698e6","html_url":"https://github.com/jchip/optional-require","commit_stats":{"total_commits":48,"total_committers":7,"mean_commits":6.857142857142857,"dds":"0.22916666666666663","last_synced_commit":"977328d6e8a342fc214805586a32cb0044890640"},"previous_names":[],"tags_count":12,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jchip%2Foptional-require","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jchip%2Foptional-require/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jchip%2Foptional-require/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jchip%2Foptional-require/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/jchip","download_url":"https://codeload.github.com/jchip/optional-require/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":243532544,"owners_count":20306157,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["nodejs","optional","require"],"created_at":"2024-10-12T14:08:04.890Z","updated_at":"2025-03-17T00:32:43.295Z","avatar_url":"https://github.com/jchip.png","language":"TypeScript","readme":"[![NPM version][npm-image]][npm-url] [![Build Status][travis-image]][travis-url]\n[![Dependency Status][daviddm-image]][daviddm-url] [![devDependency Status][daviddm-dev-image]][daviddm-dev-url]\n\n# Optional Require\n\nnode.js 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.\n\n## Why not try/catch?\n\nSo why not just do:\n\n```ts\nlet some;\ntry {\n  some = require(\"some-optional-module\");\n} catch {\n  // do nothing\n}\n```\n\n1. You need to keep the variable outside: `let some` before try/catch\n2. If `\"some-optional-module\"` contains error itself, above code will silently ignore it, leaving you, and more importantly, your users, puzzling on why it's not working.\n\n## Usage\n\nTypeScript:\n\n```ts\nimport { optionalRequire } from \"optional-require\";\n\nconst some = optionalRequire(\"some-optional-module\");\n```\n\nJavaScript:\n\n```js\nconst { optionalRequire } = require(\"optional-require\");\n\nconst foo = optionalRequire(\"foo\") || {};\nconst bar = optionalRequire(\"bar\", true); // true enables console.log a message when not found\nconst xyz = optionalRequire(\"xyz\", \"test\"); // \"test\" enables console.log a message with \"test\" added.\nconst fbPath = optionalRequire.resolve(\"foo\", \"foo doesn't exist\");\n// relative module path works - *but* you need to pass in `require` from your file\nconst rel = optionalRequire(\"../foo/bar\", { require });\n```\n\n### Binding `require`\n\nThe default `optionalRequire` uses `require` from the context of this module. While you can pass in your `require` in `options`, if you want to create your own function that's bound to your `require`, you can do it with `makeOptionalRequire`:\n\n```ts\nimport { makeOptionalRequire } from \"optional-require\";\n\nconst optionalRequire = makeOptionalRequire(require);\n\n// now you can optional require files in same dir as your file\nconst myModule = optionalRequire(\"./my-module\");\n```\n\n### Legacy Usage\n\nIn older versions, this module exports `makeOptionalRequire` directly and this is the legacy usage in JavaScript, which is still supported:\n\n```js\nconst optionalRequire = require(\"optional-require\")(require);\n\nconst foo = optionalRequire(\"foo\") || {};\nconst bar = optionalRequire(\"bar\", true); // true enables console.log a message when not found\nconst xyz = optionalRequire(\"xyz\", \"test\"); // \"test\" enables console.log a message with \"test\" added.\nconst fbPath = optionalRequire.resolve(\"foo\", \"foo doesn't exist\");\nconst rel = optionalRequire(\"../foo/bar\"); // relative module path works\n```\n\n## API\n\n\u003chttps://jchip.github.io/optional-require/modules.html#optionalrequire\u003e\n\n# LICENSE\n\nApache-2.0 © [Joel Chen](https://github.com/jchip)\n\n[travis-image]: https://travis-ci.org/jchip/optional-require.svg?branch=master\n[travis-url]: https://travis-ci.org/jchip/optional-require\n[npm-image]: https://badge.fury.io/js/optional-require.svg\n[npm-url]: https://npmjs.org/package/optional-require\n[daviddm-image]: https://david-dm.org/jchip/optional-require/status.svg\n[daviddm-url]: https://david-dm.org/jchip/optional-require\n[daviddm-dev-image]: https://david-dm.org/jchip/optional-require/dev-status.svg\n[daviddm-dev-url]: https://david-dm.org/jchip/optional-require?type=dev\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjchip%2Foptional-require","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjchip%2Foptional-require","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjchip%2Foptional-require/lists"}