{"id":15731476,"url":"https://github.com/tuchk4/magic-require","last_synced_at":"2025-03-13T04:31:03.859Z","repository":{"id":57277421,"uuid":"50314416","full_name":"tuchk4/magic-require","owner":"tuchk4","description":"Correct node require function (according to caller dirname and current cwd) for linked (npm link) or global packages ","archived":false,"fork":false,"pushed_at":"2017-07-24T10:02:39.000Z","size":514,"stargazers_count":5,"open_issues_count":2,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-02-26T18:24:27.430Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"JavaScript","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/tuchk4.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}},"created_at":"2016-01-24T23:52:43.000Z","updated_at":"2021-08-19T15:10:13.000Z","dependencies_parsed_at":"2022-09-18T06:59:23.489Z","dependency_job_id":null,"html_url":"https://github.com/tuchk4/magic-require","commit_stats":null,"previous_names":["tuchk4/is-node-module-exists"],"tags_count":5,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tuchk4%2Fmagic-require","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tuchk4%2Fmagic-require/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tuchk4%2Fmagic-require/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tuchk4%2Fmagic-require/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/tuchk4","download_url":"https://codeload.github.com/tuchk4/magic-require/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":243341406,"owners_count":20275866,"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":[],"created_at":"2024-10-04T00:01:49.154Z","updated_at":"2025-03-13T04:31:03.526Z","avatar_url":"https://github.com/tuchk4.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"## The problem\nUsually module availability could be checked using native `require` method.\n\n```js\nconst isExists = false;\n\ntry {\n  require.resolve('angular')\n  isExists = true;\n} catch (e) {\n    \n}\n```\n\nBut here is problem: I don't want to write `try catch` statements :)\n\nOK. The real problem appears when developing component with auto requiring modules. \n`require` and `require.resolve` will work correctly until component is in local node_modules.\nAfter [npm link](https://docs.npmjs.com/cli/link) (that is very useful while developing npm packages) or if components\nis installed as global package - `require` and `require.resolve` will work from global context (at \\*nix systems\n`/usr/local/lib/node_modules`). \n\nFor example:\nI am developing build / server like webpack. And there are loaders (babel-loader, html-loader, json-loader etc).\nAnd I want my builder to require loader automatically when it is needed. So my `node_modules` should be as below:\n\n    /my-work-directory/node_modules\n                             |-builder/\n                             |-babel-loader/\n                             |-html-loader/\n                             |-json-loader/\n         \nIn this case if I run `require('babe-loader')` at `builder` component - it will work.\nBut If I `npm link` my build (for better developing process) or make in global - my `node_modules` will be as below:\n\n    /usr/local/lib/node_modules\n                            |-builder/\n        \n\n    /my-work-directory/node_modules\n                             |-(linked directory) builder/\n                             |-babel-loader/\n                             |-html-loader/\n                             |-json-loader/\n\nAnd in this case if I run `require('babe-loader')` at `builder` - will be exception `Error: Cannot find module 'babel-loader'`.\nThis happens because `require` depends on **\\_\\_dirname** that equals `/usr/local/lib/node_modules/builder` at `builder` component. \nHere is documentation how [modules are loading from node_modules folder](https://nodejs.org/docs/latest/api/modules.html#modules_loading_from_node_modules_folders)\n\n## Solution\n\n`npm install --save magic-require`\n\nFirst resolve modules according to caller module **\\_\\_dirname** and if not resolved - try to resolve according to current `process.cwd()`. \n100% supports `package.json` if [loads module as directory](https://nodejs.org/docs/latest/api/modules.html#modules_all_together).\n\nExamples\n\n```js\nimport magic from 'magic-require';\n\n\nmagic.isExists('babel-loader'); // true\nmagic.isExists('expose-loader'); // false\n\nmagic.resolve('babel-loader'); // ../../../.. /my-work-directory/node_modules/babe-loader/index.js\n\nconst BabelLoader = magic('babel-laoder');\n```\n\nAvailable methods\n\n- `isExists(module)` - returns **true** if module exists and **false** - if not.\n- `resolve(module)` - returns relative path to requested module from current executing module's **\\_\\_dirname** or `null`. \n\n\n## Community\nYou are always welcome for ideas and pull requests :)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftuchk4%2Fmagic-require","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ftuchk4%2Fmagic-require","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftuchk4%2Fmagic-require/lists"}