{"id":13787023,"url":"https://github.com/bridgetownrb/node-runner","last_synced_at":"2025-04-13T17:06:29.813Z","repository":{"id":56885652,"uuid":"260118464","full_name":"bridgetownrb/node-runner","owner":"bridgetownrb","description":"A simple way to execute Javascript in a Ruby context via Node","archived":false,"fork":false,"pushed_at":"2024-04-05T20:22:22.000Z","size":29,"stargazers_count":34,"open_issues_count":2,"forks_count":4,"subscribers_count":4,"default_branch":"master","last_synced_at":"2025-04-13T17:05:58.622Z","etag":null,"topics":["bridgetown-automation","bridgetown-plugin","javascript","json","node","nodejs","ruby"],"latest_commit_sha":null,"homepage":null,"language":"Ruby","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/bridgetownrb.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE.txt","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2020-04-30T05:06:08.000Z","updated_at":"2025-03-29T00:15:01.000Z","dependencies_parsed_at":"2023-02-09T16:16:09.897Z","dependency_job_id":null,"html_url":"https://github.com/bridgetownrb/node-runner","commit_stats":null,"previous_names":[],"tags_count":3,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bridgetownrb%2Fnode-runner","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bridgetownrb%2Fnode-runner/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bridgetownrb%2Fnode-runner/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bridgetownrb%2Fnode-runner/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/bridgetownrb","download_url":"https://codeload.github.com/bridgetownrb/node-runner/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248750075,"owners_count":21155685,"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":["bridgetown-automation","bridgetown-plugin","javascript","json","node","nodejs","ruby"],"created_at":"2024-08-03T20:00:24.883Z","updated_at":"2025-04-13T17:06:29.773Z","avatar_url":"https://github.com/bridgetownrb.png","language":"Ruby","readme":"# NodeRunner for Ruby\n\nA simple way to execute Javascript in a Ruby context via Node. (Loosely based on the Node Runtime module from [ExecJS](https://github.com/rails/execjs).)\n\n[![Gem Version](https://badge.fury.io/rb/node-runner.svg)](https://badge.fury.io/rb/node-runner)\n\n## Installation\n\nRun this command to add this plugin to your project's Gemfile:\n\n```shell\n$ bundle add node-runner\n```\n\nFor [Bridgetown](https://www.bridgetownrb.com) websites, you can run an automation to install NodeRunner and set up a builder plugin for further customization.\n\n```shell\nbin/bridgetown apply https://github.com/bridgetownrb/node-runner\n```\n\n## Usage\n\nSimply create a new `NodeRunner` object and pass in the Javascript code you wish to\nexecute:\n\n```ruby\nrequire \"node-runner\"\n\nrunner = NodeRunner.new(\n  \u003c\u003c~JAVASCRIPT\n    const hello = (response) =\u003e {\n      return `Hello? ${response}`\n    }\n  JAVASCRIPT\n)\n```\n\nThen call the function as if it were a genuine Ruby method:\n\n```ruby\nrunner.hello \"Goodbye!\"\n\n# output: \"Hello? Goodbye!\"\n```\n\nUnder the hood, the data flowing in and out of the Javascript function is translated via JSON, so you'll need to stick to standard JSON-friendly data\nvalues (strings, integers, arrays, hashes, etc.)\n\nYou can also use Node require statements in your Javascript:\n\n```ruby\nrunner = NodeRunner.new(\n  \u003c\u003c~JAVASCRIPT\n    const path = require(\"path\")\n    const extname = (filename) =\u003e {\n      return path.extname(filename);\n    }\n  JAVASCRIPT\n)\n  \nextname = runner.extname(\"README.md\")\n\nextname == \".md\"\n\n# output: true\n```\n\nMultiple arguments for a function work, as do multiple function calls (aka\nif you define `function_one` and `function_two` in your Javascript, you can call\n`runner.function_one` or `runner.function_two` in Ruby).\n\n## Node Executor Options\n\nIf you need to customize which `node` binary is executed, or wish to use your\nown wrapper JS to bootstrap the `node` runtime, you can pass a custom instance\nof `NodeRunner::Executor` to `NodeRunner`:\n\n```ruby\nNodeRunner.new \"…\", executor: NodeRunner::Executor.new(command: \"/path/to/custom/node\")\n```\n\n`command` can be an array as well, if you want to attempt multiple paths until one is found. Inspect the `node-runner.rb` source code for more information on the available options.\n\n## Caveats\n\nA single run of a script is quite fast, nearly as fast as running a script directly with the\n`node` CLI…because that's essentially what is happening here. However, the performance characteristics using\nthis in a high-traffic request/response cycle (say, from a Rails app) is unknown. Likely the best context to use\nNode Runner would be via a background job, or during a build process like when using a static site generator.\n(Like our parent project [Bridgetown](https://github.com/bridgetownrb/bridgetown)!)\n\n## Testing\n\n* Run `bin/rake` to run the test suite.\n\n## Contributing\n\n1. Fork it (https://github.com/bridgetownrb/node-runner/fork)\n2. Create your feature branch (`git checkout -b my-new-feature`)\n3. Commit your changes (`git commit -am 'Add some feature'`)\n4. Push to the branch (`git push origin my-new-feature`)\n5. Create a new Pull Request\n","funding_links":[],"categories":["Official Resources","Ruby"],"sub_categories":["Official Automation's"],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbridgetownrb%2Fnode-runner","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fbridgetownrb%2Fnode-runner","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbridgetownrb%2Fnode-runner/lists"}