0

Im trying to run a C file using child process in node.js. Im using execFile to do this, the 'test' file is in the same folder. The same program runs on linux and gives the output but doesn't run on windows. It shows error: spawn ./test ENONET on windows. What do I do?

const { execFile } = require('child_process')
execFile('./test', (error, stdout, stderr) => {
if(error) {
    console.log(`error: ${error.message}`)
    return
}
if(stderr) {
    console.log(`stderr: ${stderr}`)
}
console.log(`stdout: ${stdout}`) })

Edit: I tried it with this file 'somefile.sh' which is in the same directory

#!/bin/bash
//ls -lh //for linux
-dir //for windows

It shows

Error: spawn UNKNOWN
←[90m    at ChildProcess.spawn (internal/child_process.js:403:11)←[39m
←[90m    at spawn (child_process.js:553:9)←[39m
←[90m    at execFile (child_process.js:237:17)←[39m
    at Object.<anonymous> (C:\Users\Dell\Desktop\Projects\childprocess\exec_file.js:6:1)
←[90m    at Module._compile (internal/modules/cjs/loader.js:1063:30)←[39m
←[90m    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1092:10)←[39m
←[90m    at Module.load (internal/modules/cjs/loader.js:928:32)←[39m
←[90m    at Function.Module._load (internal/modules/cjs/loader.js:769:14)←[39m
←[90m    at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:72:12)←[39m
←[90m    at internal/main/run_main_module.js:17:47←[39m {
  errno: ←[33m-4094←[39m,
  code: ←[32m'UNKNOWN'←[39m,
  syscall: ←[32m'spawn'←[39m
}

I also tried for exec using a single command and it gives the output for the below code.

    const {exec} = require('child_process')

exec('pwd', (error, stdout, stderr) => {
    if(error) {
        console.log(`error: ${error.message}`)
        return
    }
    if(stderr) {
        console.log(`stderr: ${stderr}`)
    }
    console.log(`stdout: ${stdout}`)
})

Output shows the present working direactory. This is the tutorial I referred: https://www.youtube.com/watch?v=bbmFvCbVDqo&t=441s

Nvv
  • 79
  • 10
  • And your node & compiled c file are in the same directory? Does the file have a extension? e.g.`.exe`? Have you tried it with a file extension? – Marc Feb 08 '22 at 14:30
  • Yes, same directory. But the file originally doesn't have an extension. When I run it as './test.c' and './test.exe' it still shows the same error. – Nvv Feb 09 '22 at 05:57
  • Can you add the c source or a simple c dummy that i can try it by myself? Have you tried it with other methods from the `child_process module, like `exec`? – Marc Feb 09 '22 at 06:09
  • Yes i've tried exec and gave pwd command, it works for exec but not execfile. I've added the sample code in edit. – Nvv Feb 09 '22 at 06:28

0 Answers0