{"id":15942672,"url":"https://github.com/suchipi/commonjs-standalone","last_synced_at":"2025-10-19T04:30:58.526Z","repository":{"id":57204417,"uuid":"154397849","full_name":"suchipi/commonjs-standalone","owner":"suchipi","description":"Standalone CommonJS loader for any JS engine","archived":false,"fork":false,"pushed_at":"2022-04-19T04:25:27.000Z","size":250,"stargazers_count":5,"open_issues_count":0,"forks_count":1,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-01-29T08:11:13.888Z","etag":null,"topics":["browser","commonjs","embedded-js","export","import","javascript","loader","module","node-js","require"],"latest_commit_sha":null,"homepage":null,"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/suchipi.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":"2018-10-23T21:15:44.000Z","updated_at":"2022-04-19T04:25:31.000Z","dependencies_parsed_at":"2022-09-18T01:30:51.787Z","dependency_job_id":null,"html_url":"https://github.com/suchipi/commonjs-standalone","commit_stats":null,"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/suchipi%2Fcommonjs-standalone","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/suchipi%2Fcommonjs-standalone/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/suchipi%2Fcommonjs-standalone/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/suchipi%2Fcommonjs-standalone/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/suchipi","download_url":"https://codeload.github.com/suchipi/commonjs-standalone/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":237068384,"owners_count":19250057,"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","commonjs","embedded-js","export","import","javascript","loader","module","node-js","require"],"created_at":"2024-10-07T08:00:43.852Z","updated_at":"2025-10-19T04:30:53.240Z","avatar_url":"https://github.com/suchipi.png","language":"JavaScript","readme":"# `commonjs-standalone`\n\n`commonjs-standalone` is a standalone CommonJS loader for use in any JavaScript engine. You give it a way to **resolve**, **read**, and **run** modules, and it will give you the `module`, `exports`, `require`, `__filename`, `__dirname` system you're familiar with from Node.js.\n\n## How it works\n\nThis module exports one function called `requireMain`. Given the absolute path to a module and a `Delegate` object, `requireMain` will load the module at that path. You must provide your own `Delegate` object that `commonjs-standalone` will use to **resolve**, **read**, and **run** modules. A `Delegate` object has this shape:\n\n```js\ntype Delegate = {\n  // A module at filepath `fromFilePath` is trying to require `id`.\n  // Resolve `id` into an absolute path, or throw an error if it can't be found.\n  resolve(id: string, fromFilePath: string): string,\n\n  // Read the contents of the file at `filepath` and return them as a string.\n  read(filepath: string): string,\n\n  // Run this code using the provided module environment object. The filepath\n  // is provided for your information; for configuring stack traces, or if you\n  // want to compile JSON to JS, etc.\n  run(\n    code: string,\n    moduleEnv: {\n      module: Object,\n      exports: Object,\n      require: Function,\n      __filename: string,\n      __dirname: string\n    },\n    filepath: string\n  ): void\n};\n```\n\nHow you **resolve**, **read**, and **run** modules will vary depending on your engine, which is why those things are left up to you.\n\n## Examples\n\nHere's an example of a very simple `Delegate` that keeps modules in a JavaScript object:\n\n```js\n// This delegate loads modules from a JavaScript Object.\nconst modules = {\n  \"module-one\": \"require('module-two');\",\n  \"module-two\": \"console.log('hi from module-two');\"\n};\n\nconst delegate = {\n  resolve(id, fromFilePath) {\n    // Normally you would use `fromFilePath` to resolve relative file paths,\n    // but in this example, only absolute paths are supported, so nothing\n    // needs to be resolved.\n    return id;\n  },\n\n  read(filepath) {\n    return modules[filepath];\n  },\n\n  run(code, moduleEnv, filepath) {\n    const wrapper = eval(\n      \"(function (exports, require, module, __filename, __dirname) { \" +\n        code +\n        \"\\n})\"\n    );\n    wrapper(\n      moduleEnv.exports,\n      moduleEnv.require,\n      moduleEnv.module,\n      moduleEnv.__filename,\n      moduleEnv.__dirname\n    );\n  }\n};\n\nrequireMain(\"module-one\", delegate); // logs 'hi from module-two'\n```\n\nHere's a more complex `Delegate` that uses [the `resolve` package from npm](https://npm.im/resolve) to resolve modules and node's `fs` and `vm` modules to read and run them:\n\n```js\nconst fs = require(\"fs\");\nconst path = require(\"path\");\nconst vm = require(\"vm\");\nconst resolve = require(\"resolve\");\n\nconst delegate = {\n  resolve(id, fromFilePath) {\n    return resolve.sync(id, {\n      basedir: path.dirname(fromFilePath),\n      preserveSymlinks: false\n    });\n  },\n\n  read(filepath) {\n    return fs.readFileSync(filepath, \"utf-8\");\n  },\n\n  run(code, moduleEnv, filepath) {\n    const wrapper = vm.runInThisContext(\n      \"(function (exports, require, module, __filename, __dirname) { \" +\n        code +\n        \"\\n})\",\n      { filename: filepath }\n    );\n    wrapper(\n      moduleEnv.exports,\n      moduleEnv.require,\n      moduleEnv.module,\n      moduleEnv.__filename,\n      moduleEnv.__dirname\n    );\n  }\n};\n```\n\n## API Documentation\n\n### `requireMain(filepath: string, delegate: Delegate): void`\n\n`requireMain` is exported from `commonjs-standalone` as a named export. It loads the first module (also called the main module), which will load other modules using its `require` function.\n\nIt should be called with an **absolute** path to a file to load, and a `Delegate` that it will use to **resolve**, **read**, and **run** modules.\n\n```js\nfunction requireMain(filepath: string, delegate: Delegate);\n```\n\n### `Delegate`\n\nA `Delegate` is an object with three functions on it: `resolve`, `read`, and `run`. Each are documented here.\n\n### `Delegate.resolve(id: string, fromFilePath: string): string`\n\nThis function is called when a module at the absolute filepath `fromFilePath`\nis trying to require `id`. **Resolve** `id` into an absolute path, or throw an error if it can't be found.\n\nFor example, if `/Users/suchipi/my-package/index.js` contained:\n\n```js\nrequire(\"./foo\");\n```\n\nThen `Delegate.resolve` would be called with an `id` of `\"./foo\"` and a `fromFilePath` of `\"/Users/suchipi/my-package/index.js\"`.\n\nIn that example, you would probably want to return `\"/Users/suchipi/my-package/foo.js\"` (assuming it exists).\n\n### `Delegate.read(filepath: string): string`\n\nThis function is called when the module system wants to **read** the contents of the file at the absolute path `filepath`. You should read them and return the code as a string.\n\n### `Delegate.run(code: string, moduleEnv: Object, filepath: string): void`\n\nThis function is called when the module system wants to **run** some code. It's called with:\n\n- The code to run,\n- a `ModuleEnvironment` object, and\n- the absolute path to where the module came from.\n\nThe `ModuleEnvironment` object has 5 properties on it that you should expose to the running code: `module`, `exports`, `require`, `__filename`, and `__dirname`.\n\nOne way to expose these to your code is to wrap your code in a function:\n\n```js\nconst wrappedCode =\n  \"(function (exports, require, module, __filename, __dirname) { \" +\n  code +\n  \"\\n})\";\n```\n\nThen you can pass everything from the `ModuleEnvironment` object into the function wrapper after you compile it:\n\n```js\n// You probably shouldn't use `eval`, since it leaks local variables into the scope. There is probably a way to\n// run this cleanly from your JavaScript Engine's API.\nconst wrapperFunction = eval(wrappedCode);\n\nwrapperFunction(\n  moduleEnv.exports,\n  moduleEnv.require,\n  moduleEnv.module,\n  moduleEnv.__filename,\n  moduleEnv.__dirname\n);\n```\n\n## Supported features\n\n- `module`\n  - `module.id`\n  - `module.exports`\n- `require`\n  - `require.resolve`\n  - `require.cache` and deleting from `require.cache`\n- `exports`\n- `__filename`\n- `__dirname`\n- Circular dependencies\n\n## License\n\nMIT\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsuchipi%2Fcommonjs-standalone","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsuchipi%2Fcommonjs-standalone","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsuchipi%2Fcommonjs-standalone/lists"}