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 });
Green
  • 28,742
  • 61
  • 158
  • 247

2 Answers2

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.

NullDev
  • 6,739
  • 4
  • 30
  • 54
  • I did. It blocks terminal. Terminal just hangs. No outputs, no event handlers starts. To break it, I run `Ctrl+C`. – Green Jan 11 '18 at 16:59
  • @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"? – NullDev Jan 11 '18 at 17:00
2

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

Akhil Thayyil
  • 9,263
  • 6
  • 34
  • 48
  • Yes, you are right. Passing an absolute path solved the issue. `path.resolve(__dirname, 'service.linux')` – Green Jan 11 '18 at 17:11