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

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.

Related

python script in a typescript file

how do I use my python script in a typescript file? Like how do I link it ? I have to traverse a xml file which I have a python code which updates some details and wanted to use it in the typescript server file.
Like the server should update some details in a xml file. this update functionality is implemented in python and I wanted to use it in the typescript server code.
You can run the python script in node using exec(): https://nodejs.org/api/child_process.html#child_processexeccommand-options-callback
A minimal example assuming you have python3 installed on the machine running your node script could be:
// shell.py
print('printed in python')
// server.js
import {exec} from 'child_process'
//if you don't use module use this line instead:
// const { exec } = require('child_process')
exec('python3 shell.py', (error, stdout, stderr) => {
if (error) {
console.log(`error: ${error.message}`);
}
else if (stderr) {
console.log(`stderr: ${stderr}`);
}
else {
console.log(stdout);
}
})
// using it in the shell
% node server.js
// should output this:
printed in python
This way you could traverse your XML file, change it and output it to the js/ts file, or save it as a file and just read that via your js/ts code.

How to run a script from another path?

I am creating a nodejs package and within the program, a bash script is executed.
The bash script "./helpers/script.sh" should be executed via a relative path, which means when i install the package and run it, the script should be loaded from 'appdata/roaming/npm/mypackage/helpers/script.sh' and not the current working directory.
I have tried using path.relative() and path.resolve() but with no help because it keeps trying to find the script file in my CWD.
exec(`bash ./helpers/init.sh`, (error, stdout, stderr) => {
if (error === null) {
console.log('Done.')
// do something
} else {
console.log(error)
}
})
Any help would be appreciated.
If I understood it correctly and you are trying to call init.sh from script.sh located in the same directory, then, I'd try the following:
// script.sh
const path = require('path');
const initAbsPath = path.resolve('./init.sh');
exec(`bash ${initAbsPath}`, (error, stdout, stderr) => {
// ...
});

How to run bash commands from Node.js and get result

I want to run a bash script on my server using a Node js function and get the result back in Node through a callback. Sample script is as follows -
grep -c "eventName" 111data.csv
Is this possible? I looked at the following module but looks very complex. Does anyone know of a way this can be done?
https://www.npmjs.com/package/bashjs
You can execute a command
const { exec } = require('child_process');
const grep = exec('grep -c "eventName" 111data.csv', function (error, stdout, stderr) {
if (error) {
console.log(error.stack);
console.log('Error code: '+error.code);
console.log('Signal received: '+error.signal);
}
console.log('Child Process STDOUT: '+stdout);
console.log('Child Process STDERR: '+stderr);
});
grep.on('exit', function (code) {
console.log('Child process exited with exit code '+code);
});
Just modified the example from the Node.js article. Didn't actually test it

I am trying to execute a file in sh with nodejs but the console tells me that the file cannot be found

This is the code that i found on the internet:
const { exec } = require('child_process');
var yourscript = exec('sh launch.sh', (error, stdout, stderr) => {
console.log(stdout);
console.log(stderr);
if (error !== null) {
console.log(`exec error: ${error}`);
}
});
and here is my sh file which is in the same file as my previous code:
echo "Hi There!"
but each time I run the program the console shows me:
exec error: Error: Command failed: sh launch.sh
sh: 0: Can't open launch.sh
Can somebody help me with my code please?
First, you seem to have a file permissions issue, so to fix that, start by setting the file permissions for the user as the user needs execution permissions:
chmod 755 launch.sh
Second, the code you found online is using "exec" that is now deprecated in favour of builtin child_process.execFile. So, for your needs the code would look a bit like:
var child_process = require('child_process');
child_process.exec('sh launch.sh', function(error, stdout, stderr){
console.log(stdout);
});
This should help to get started! The inner command sh launch.sh should be executable in the command line, directly, so tested without running the main host or nodejs script! Type the command in your CLI and press enter! Alternatively to calling sh launch.sh do /bin/sh launch.sh
Of course, you might want to look at docs to improve the implementation and fully understand file permissions under your OS, what sh means and finally child_process.exec.
https://www.linux.com/tutorials/understanding-linux-file-permissions
https://en.wikipedia.org/wiki/Bourne_shell
https://nodejs.org/api/child_process.html

How to run windows cmd.exe commands from javascript code run on node bash?

Is there a way where I can invoke a windows batch file from inside the javascript code? Or any other healthy way to do the below through any node package?
scripts.bat
ECHO "JAVASCRIPT is AWESOME"
PAUSE
scripts.js
// Code to read and run the batch file //
On the command prompt:
C:/> node scripts.js
One way to do this is with child_process. You just have to pass the file you want to execute.
const execFile = require('child_process').execFile;
const child = execFile('scripts.bat', [], (error, stdout, stderr) => {
if (error) {
throw error;
}
console.log(stdout);
});

Categories