[英]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: 我有一个问题,我正在尝试执行使用nodemailer发送邮件的文件,我需要从另一个JS文件执行该文件,我试图这样做:
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 .
我不需要发送send.js文件的参数,只需发送具有完全指定路径的文本文件即可。 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: 您需要指定一个回调函数,该函数将在您的exec命令执行后被调用:
i created 2 files: 我创建了2个文件:
anotherTest.js anotherTest.js
console.log('another test');
test.js 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. 您可以通过在终端/控制台中执行“ node test.js ”来运行test.js脚本。 you can change the arguments of the exec command with the arguments that you want.
您可以使用所需的参数更改exec命令的参数。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.