5

I have a problem, I'm trying to execute file that sending mail using nodemailer and I need to execute it from another JS file I tried to do it like this:

const  exec  = require('child_process').exec;
       exec('"C:/Users/NikitaSeliverstov/node_modules/.bin/send.js"');

but mail is not sending. I don't need to send params the file send.js just sending text file with fully specified path . Sorry for obvious question but I can't figure it out. Also I tried to do it like this:

 const  exec  = require('child_process').exec;
        exec('"node C:/Users/NikitaSeliverstov/node_modules/.bin/send.js"');

1 Answers1

8

you need to specify a callback function which will be called after your exec command is executed:

i created 2 files:

anotherTest.js

console.log('another test');

test.js

const exec = require('child_process').exec;

const child = exec('node anotherTest.js',
    (error, stdout, stderr) => {
        console.log(`stdout: ${stdout}`);
        console.log(`stderr: ${stderr}`);
        if (error !== null) {
            console.log(`exec error: ${error}`);
        }
});

this is the output:

stdout: another test

stderr: 

you run the test.js script by doing "node test.js" in the terminal/console. you can change the arguments of the exec command with the arguments that you want.

Hussain Ali Akbar
  • 1,585
  • 2
  • 16
  • 28