3

First, similar questions, no answers:

Node.js child_process.spawn() fail to run executable file

node.js child_process.spawn ENOENT error - only under supervisord

I have an executable file with .linux extension. It is http server.

service.linux

I can run it like this:

$ ./service.linux
2018/01/11 18:32:56 listening on port 8080

But since it is not a command, I cannot start it as a spawned process:

let cp = spawn('service.linux', [], { cwd: __dirname });

The errors I get is:

service.linux: command not found

ERROR: Error: spawn service.linux ENOENT

How can I run it as a command? Or should I use some other command to run it, like:

$ start service.linux

UPD:

$ file service.linux 
service.linux: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), statically linked, not stripped

UPD:

It needs absolute path:

const path = require('path');
let cp = spawn(path.resolve(__dirname, `service.linux`), [], { cwd: __dirname });
6
  • paste the contents of service.linux, which interpreter is being used to run service.linux ? Commented Jan 11, 2018 at 16:44
  • @AkhilThayyil, I can't. Its some binary file. Commented Jan 11, 2018 at 16:47
  • run this command "file service.linux" check and share the mime type Commented Jan 11, 2018 at 16:47
  • Updated the question. ELF 64-bit LSB executable, x86-64, version 1 (SYSV), statically linked, not stripped Commented Jan 11, 2018 at 16:51
  • Have you tried using the relative path? i.e.: let cp = spawn('./service.linux', [], { cwd: __dirname }); Commented Jan 11, 2018 at 16:56

2 Answers 2

3

Try using exec and also write ./ before the name of the binary:

const { exec } = require("child_process");

exec("./service.linux", (err, data) => {
    if (err) return console.log(err);
    console.log(data);
});

Assuming the file is in the same directory as the script.

The error ENOENT means "Error No Entry" so it basically doesn't find the command.

That's why we specify "./". That way it will handle it as a path.

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

2 Comments

I did. It blocks terminal. Terminal just hangs. No outputs, no event handlers starts. To break it, I run Ctrl+C.
@Green It doesn't really make the terminal hang. It just means that there is no data to show. Can you verify if the binary is running while the terminal "blocks"?
2

This is a path issue, node is unable to find service.linux file, use absolute path, issue will be resolved

1 Comment

Yes, you are right. Passing an absolute path solved the issue. path.resolve(__dirname, 'service.linux')

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.