Node JS child process error The "message" argument must be defined

494 views Asked by At

While Trying to use child Process to have a multiple ssh connection I got this erroryour text `

inside process.on
node:internal/child_process:754
      throw new ERR_MISSING_ARGS('message');
      ^

TypeError [ERR_MISSING_ARGS]: The "message" argument must be specified
    at new NodeError (node:internal/errors:372:5)
    at process.target._send (node:internal/child_process:754:13)
    at process.target.send (node:internal/child_process:739:19)
    at process.<anonymous> (C:\Users\dell\Desktop\Sublist3e\functions\subdomains.js:16:13)

`

I was trying to connect to ssh using child process.Let me know if this is possible and help me find the solution

process.on('message', (message) => {
    let subfind = findSubs(message.URl);
    console.log('inside process.on')
    // send the results back to the parent process
    process.send(subfind);
    // kill the child process
    process.exit();
})


function findSubs(URl) {
    ssh
        .connect({
            host: '192.168.199.131',
            username: 'aavash',
            password: 'admin'
        })
        .then(() => {
        
            return ssh.execCommand(`subfinder -d ${URl} -o /home/aavash/Desktop/${URl}.txt`)
                .then((result) => {
                    console.log(result.stdout)
                })
        })
        .then(() => {
            // Local, Remote
            return ssh.getFile(`C://Users/dell/Desktop/Sublist3e/Subdomains/${URl}.txt`, `/home/aavash/Desktop/${URl}.txt`).then(function (Contents) {
                console.log("The File's contents were successfully downloaded")
            })
         })
         .then(()=>  {
                          
             fs.readFileSync(`C://Users/dell/Desktop/Sublist3e/Subdomains/${URl}.txt`, function (err, data) {
                if (err) throw err;
                subs = data.toString().split("\n");
    

                console.log(subs)
                for(i in subs) {
                    console.log(subs[i]);
                }    
               
            })
         })    
        
     }


I call the above file as shown below:

app.get('/test', (req, res) => {
  URl='hackerrank.com'
  const child = childProcess.fork('./functions/subdomains.js');
child.send(URl)
child.on('message', (message) => res.render('subdomains', { Domain: URl, subdomains: subs }) )
})

There might be multiple erros in the code

1

There are 1 answers

2
Sanket On BEST ANSWER

You are not returning anything from findSubs. Therefore the call to findSubs in let subfind = findSubs(message.URl); returns undefined which you are passing in process.send(subfind);

process.on('message', (message) => {
    /// findSubs(message.URl); returns undefined 
    let subfind = findSubs(message.URl);
    /// subfind = undefined


    console.log('inside process.on')
    // send the results back to the parent process


    /// this line amounts to process.send(undefined); because subfind = undefined
    process.send(subfind);
    ///Hence you get the error: TypeError [ERR_MISSING_ARGS]: The "message" argument must be specified


    // kill the child process
    process.exit();
})

Return the contents of the file from the function findSubs(). Use async await for the ssh calls and the file reading in the function findSubs() and return the contents of the file.

also await findSubs() in the message event callback of the child process

process.on('message', async (message) => {
    // await findSubs here
    let subfind = await findSubs(message.URl);
    console.log('inside process.on')
    // send the results back to the parent process
    process.send(subfind);
    // kill the child process
    process.exit();
})