简体   繁体   中英

how to execute js script file as child process node.js

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"');

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM