How to Invoke Ruby from Node.Js

How can I invoke Ruby from Node.js?

You can invoke Ruby like any other shell command using child_process.exec()

var exec = require("child_process").exec;

exec('ruby -e "puts \'Hello from Ruby!\'"', function (err, stdout, stderr) {
console.log(stdout);
});

Don't know if that's what you're looking for?

Node.js read and execute a Ruby file?

Really simple by using child_process#exec

var exec = require('child_process').exec

exec('./script.rb', function (error, stdout, stderr) {
console.log('stdout: ' + stdout);
console.log('stderr: ' + stderr);
console.log('error: ' + error);
});

Docs here: child_process.exec

Async function into Nodejs call ruby file

This seems to be a ruby error. Maybe using the path module and get full path will solve this problem, you already tried it?

I have tried it:

const { exec } = require('child_process')

async function run() {
exec('ruby test.rb', (err, stdout, stderr) => {
console.log(err, `ruby returns ${stdout}`)
});
}

run()

All good with this async code... It really seems to be your ruby file is not there, if you run node server.js, your ruby file will must be in the same dir, because your current dir to exec is your base dir.

Ruby subprocess with node.js

Try STDOUT.flush on the ruby side after STDOUT.write,
as the output is being buffered.

How to pass or output data from a ruby script to a node.js script?

You can use child_process exec to execute a script and handle it's output.

Ruby Script

# example.rb
puts "hello world"

Node Script

// example.js
const exec = require('child_process').exec

exec('ruby example.rb', function(err, stdout, stderr) {
console.error(err)
console.error('stderr: ' + stderr)
console.log('stdout: ' + stdout) // logs "hello world"
});

Running rails console command from nodejs

I think what you want is rails runner instead of console. Either pass it a line of ruby code or a filename. It will run in the rails environment, not just the ruby irb environment.

How can I package Ruby code in an NPM package?

I don't think you can/should specify the Ruby version to use when executing your code. That should be up the library consumer so choose. Since you want to execute your code with exec, the library consumer will have the added responsbility of making ruby accessible to the node process. How that happens is not up to you as the library developer.

As for dependencies/gemsets, just use bundler.



Related Topics



Leave a reply



Submit