{"id":18256179,"url":"https://github.com/serapath/npm-require","last_synced_at":"2025-07-27T04:46:26.364Z","repository":{"id":65465154,"uuid":"86030568","full_name":"serapath/npm-require","owner":"serapath","description":" synchronous global require function for the browser to load commonjs modules from a (relative) path, a URL or npm","archived":false,"fork":false,"pushed_at":"2018-10-08T18:28:41.000Z","size":13,"stargazers_count":5,"open_issues_count":1,"forks_count":2,"subscribers_count":3,"default_branch":"master","last_synced_at":"2024-04-25T16:42:18.943Z","etag":null,"topics":["browser","browser-cdn","browserify","browserify-cdn","install","npm","require"],"latest_commit_sha":null,"homepage":"https://serapath.github.io/npm-require","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/serapath.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2017-03-24T05:28:12.000Z","updated_at":"2020-06-11T17:20:40.000Z","dependencies_parsed_at":"2023-01-24T20:45:18.556Z","dependency_job_id":null,"html_url":"https://github.com/serapath/npm-require","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/serapath%2Fnpm-require","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/serapath%2Fnpm-require/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/serapath%2Fnpm-require/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/serapath%2Fnpm-require/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/serapath","download_url":"https://codeload.github.com/serapath/npm-require/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":223151417,"owners_count":17096107,"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":["browser","browser-cdn","browserify","browserify-cdn","install","npm","require"],"created_at":"2024-11-05T10:20:28.252Z","updated_at":"2024-11-05T10:20:28.759Z","avatar_url":"https://github.com/serapath.png","language":"JavaScript","readme":"# npm-require\nsynchronous global require function for the browser to load commonjs modules from a (relative) path, a URL or npm\n\nhttps://www.npmjs.com/package/npm-require\n\n# demo\n```html\n\u003c!doctype html\u003e\n\u003chtml\u003e\n  \u003chead\u003e\n    \u003cmeta charset=\"utf-8\"\u003e\n    \u003cscript src=\"src/npm-require.js\"\u003e\u003c/script\u003e\n  \u003c/head\u003e\n  \u003cbody\u003e\n    \u003cscript\u003e\n      var minixhr = require('minixhr') // from npm\n      var pkg = require('./package.json')\n      var demo = require('./demo') // resolves to `./demo.js`\n      demo(pkg)\n    \u003c/script\u003e\n  \u003c/body\u003e\n\u003c/html\u003e\n```\n\n\u003c!--\n@TODO: what about internal/local non-published modules?\n\n@TODO: what about more options for src=\"npm-require#...?...\"\n  - so that different registries (or registry proxies) can be set\n  - and all channeled through an iframe/worker for caching?\n\n# DEMO (TEST)\nStructure:\n- no folders for different versions\n  - current tests are for current versions\n  - old tests are in version history\n- tests are numbered feature folders\n  - new features get the next version number\n  - retired features get deleted, thus archived into the version histroy\n- a feature folder potentially contains `fixture` folder\n- a feature folder contains a `spec.js` file to describe the feature usage\n\n# TODO\n```js\n// @TODO: make test/test.html test stuff\n// @TODO: make test/benchmark.html test stuff\n// @TODO: include {test,benchmark}.html as iframes in index.js - where `index.html` is the DEMO PAGE which includes demo and documentation\n\n// @TODO: put `src/index.js` structure into place\n// @TODO: experiment with offlineFirst/ServiceWorker with cross-domain-re-use of modules\n// @TODO: find `package.json` and load all dependencies with their versions\n// @TODO: require.config(opts) // to change NPM CDN, etc...\n// @TODO: require.packageJSON // to show packageJSON of current require\n// @TODO: maybe support version ranges (check unpkg and wzrd support)\n\n\n// @TODO: compare with npm-requie inofficial and implement\n\n// // then\n//   a. LOAD_AS_FILE(Y + X)\n//   //     LOAD_AS_FILE(X)\n//   // 1. If X is a file, load X as JavaScript text.  STOP\n//   // 2. If X.js is a file, load X.js as JavaScript text.  STOP\n//   // 3. If X.json is a file, parse X.json to a JavaScript Object.  STOP\n//   b. LOAD_AS_DIRECTORY(Y + X)\n//   // 1. If X/package.json is a file,\n//   //    a. Parse X/package.json, and look for \"main\" field.\n//   //    b. let M = X + (json main field)\n//   //    c. LOAD_AS_FILE(M)\n//   //    d. LOAD_INDEX(M)\n//         //   If X/index.js is a file, load X/index.js as JavaScript text.  STOP\n//         //  2. If X/index.json is a file, parse X/index.json to a JavaScript object. STOP\n//         //  3. If X/index.node is a file, load X/index.node as binary addon.  STOP\n//   //  If X/index.js is a file, load X/index.js as JavaScript text.  STOP\n//   //  2. If X/index.json is a file, parse X/index.json to a JavaScript object. STOP\n//   //  3. If X/index.node is a file, load X/index.node as binary addon.  STOP\n// 4. LOAD_NODE_MODULES(X, START=dirname(Y))\n//   // 1. let DIRS=NODE_MODULES_PATHS(START)\n//   // 2. for each DIR in DIRS:\n//   //    a. LOAD_AS_FILE(DIR/X)\n//   //    b. LOAD_AS_DIRECTORY(DIR/X)\n// 5. `throw new Error(\"Cannot find module '\u003cmodule name\u003e'\")`\n\n// @TODO: use as \"test cases\" or rather \"example output\"\n\n// require('./routes')\n//   ./package.json#main\n//   ./routes.js\n//   ./routes.json\n//   ./routes.node\n//\n// // /home/bytearcher/socket/src/server.js\n// require('async')\n//   /home/bytearcher/socket/src/node_modules/async\n//   /home/bytearcher/socket/node_modules/async\n//   /home/bytearcher/node_modules/async\n//   /home/node_modules/async\n//   /node_modules/async\n//\n//   module.paths\n//   [ '/Users/samer/learn-node/repl/node_modules',\n//     '/Users/samer/learn-node/node_modules',\n//     '/Users/samer/node_modules',\n//     '/Users/node_modules',\n//     '/node_modules',\n//     '/Users/samer/.node_modules',\n//     '/Users/samer/.node_libraries',\n//     '/usr/local/Cellar/node/7.7.1/lib/node' ]\n```\n\n# usage\n\n**`index.html`**\n\n```html\n\n  \u003cbody\u003e\n    \u003cscript src=\"https://wzrd.in/standalone/npm-require\"\u003e\u003c/script\u003e\n    \u003cscript src=\"browser.js\"\u003e\u003c/script\u003e\n  \u003c/body\u003e\n\n```\n\n**`browser.js`**\n\n```js\nvar minixhr = require('minixhr')\n\n// use module :-)\n```\n\n# resolver spec\n\n*The `require` function in nodejs does not follow the official module resolver logic, so below follows the resolver logic this module uses and what I think the logic currently used by `require` in nodejs looks like*\n\nopen issue:\n* https://github.com/nodejs/node/issues/17966\n\n**Official** (not implemented by this module)\n* nodejs#require https://nodejs.org/api/modules.html#modules_all_together\n* commonJS#require http://wiki.commonjs.org/wiki/Modules/1.1#Module_Context\n\n**Inofficial** (implemented by this module)  \nFollows official spec as closely as possible, but:\n* without nodejs CORE MODULES support\n* without .node file extension support\n\n```\nrequire(X) from module at path Y\n1. If X is a core module,\n   a. return the core module\n   b. STOP\n2. If X begins with '/'\n   a. set Y to be the filesystem root\n3. If X begins with './' or '/' or '../'\n   a. LOAD(Y + X)\n4. LOAD_NODE_MODULES(X, dirname(Y))\n5. THROW \"not found\"\n\nLOAD(X)\n1. LOAD_AS_DIRECTORY(X)\n2. LOAD_AS_FILE(X)\n3. LOAD_INDEX(X)\n\nLOAD_AS_DIRECTORY(X)\n1. If X/package.json is a file,\n   a. Parse X/package.json, and look for \"main\" field.\n   b. let M = X + (json main field)\n   c. LOAD_AS_FILE(M)\n   d. LOAD_INDEX(M)\n\nLOAD_AS_FILE(X)\n1. If X is a file, load X as JavaScript text.  STOP\n2. If X.js is a file, load X.js as JavaScript text.  STOP\n3. If X.json is a file, parse X.json to a JavaScript Object.  STOP\n4. If X.node is a file, load X.node as binary addon.  STOP\n\nLOAD_INDEX(X)\n1. If X/index.js is a file, load X/index.js as JavaScript text.  STOP\n2. If X/index.json is a file, parse X/index.json to a JavaScript object. STOP\n3. If X/index.node is a file, load X/index.node as binary addon.  STOP\n\nLOAD_NODE_MODULES(X, START)\n1. let DIRS=NODE_MODULES_PATHS(START)\n2. for each DIR in DIRS:\n   a. LOAD(DIR/X)\n\nNODE_MODULES_PATHS(START)\n1. let PARTS = path split(START)\n2. let I = count of PARTS - 1\n3. let DIRS = []\n4. while I \u003e= 0,\n   a. if PARTS[I] = \"node_modules\" CONTINUE\n   b. DIR = path join(PARTS[0 .. I] + \"node_modules\")\n   c. DIRS = DIRS + DIR\n   d. let I = I - 1\n5. return DIRS\n```\n--\u003e\n\n# license\nMIT\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fserapath%2Fnpm-require","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fserapath%2Fnpm-require","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fserapath%2Fnpm-require/lists"}