0

I have a nodejs application executing from folder A a binary X also in folder A (using child process exec). Binary X produce an output file O in folder A, so always from where is called.

I need instead the nodejs app to to execute the binary X in an arbitrational folder and produce an output O in a different folder from where the nodejs application A is being execute.

So basically I need to execute the binary as would be in different folder.

I would like to know if it is possible to do this in nodejs and how to do it? Thanks

0

1 Answer 1

1

If I understand the question correctly, You would like Binary X to output a file O in a given directory? Correct me if I'm wrong, please.

Firstly, I would look into checking if the binary has an option to specify where to output the file. If not, perhaps the best way to solve this is by setting the current working directory.

I would do the following:

import {spawn} from 'child_process';
import * as path from 'path';
import * as os from 'os';

const proc = spawn('./binary.exe', [<Array of parameters to binary>], {
  cwd: `${path.join(os.homedir, filename)}.o`
});

proc.stdout.pipe(process.stdout);

The Official Nodejs Docs

The cwd property is the active ingredient here. It specifies where the binary should be run from. The spawn function simply executes executables and binaries in much the same way a terminal does.

Hope that helps.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks you understand correctly, the binary as NO option to output the file to a specific directory
Cool. Let me know how it goes and don't forget to accept the answer if it worked. Thanks.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.