How to run a script from another path? - javascript

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) => {
// ...
});

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 can I make electron open an exe file with a button [duplicate]

I'm writing a desktop web app that uses node.js to access the local file system. I can currently use node.js to open and copy files to different places on the hard drive. What I would also like to do is allow the user to open a specific file using the application that is associated with the file type. In other words, if the user selects "myfile.doc" in a Windows environment, it will launch MSWord with that file.
I must be a victim of terminology, because I haven't been able to find anything but the spawning of child processes that communicate with node.js. I just want to launch a file for the user to view and then let them decided what to do with it.
Thanks
you can do this
var cp = require("child_process");
cp.exec("document.docx"); // notice this without a callback..
process.exit(0); // exit this nodejs process
it not safe thought, to ensure that the command show no errors or any undesired output
you should add the callback parameter
child_process.exec(cmd,function(error,stdout,stderr){})
and next you can work with events so you won't block execution of script or even make use of a external node.js script that launches and handles outputs from processes which you spawn from a "master" script.
In below example I have used textmate "mate" command to edit file hello.js, you can run any command with child_process.exec but the application you want to open file in should provide you with command line options.
var exec = require('child_process').exec;
exec('mate hello.js');
var childProcess = require('child_process');
childProcess.exec('start Example.xlsx', function (err, stdout, stderr) {
if (err) {
console.error(err);
return;
}
console.log(stdout);
process.exit(0);// exit process once it is opened
})
Emphasis on where 'exit' is called. This executes properly in windows.
Simply call your file (any file with extension, including .exe) from the command promp, or programmatically:
var exec = require('child_process').exec;
exec('C:\\Users\\Path\\to\\File.js', function (err, stdout, stderr) {
if (err) {
throw err;
}
})
If you want to run a file without extension, you can do almost the same, as follow:
var exec = require('child_process').exec;
exec('start C:\\Users\\Path\\to\\File', function (err, stdout, stderr) {
if (err) {
throw err;
}
})
As you can see, we use start to open the File, letting windows (or windows letting us) choose an application.
If you prefer opening a file with async/await pattern,
const util = require('util');
const exec = util.promisify(require('child_process').exec);
async function openFile(path) {
const { stdout, stderr, err } = await exec(path);
if (err) {
console.log(err);
return;
}
process.exit(0);
}
openFile('D:\\Practice\\test.txt'); // enter your file location here

how to execute another nodejs inside javascript code?

how to execute another js code but using nodejs,, I mean how to do like this, node index2.js inside index.js ,what should I write inside index.js
searching on internet its look like using
const { exec } = require('child_process');
but I have no Idea my brain can't get it,
pls someone give me some example if possible thanks
instead of doing that, you're working two scripts with the same language. So, if you have a function or something you can simply import it doing:
var index2 = require('./index2')
if you need to execute another script written in bash for example... you have to do this:
const { exec } = require('child_process');
exec('python example.py', (err, stdout, stderr) => {
if (err) {
// node couldn't execute the command
return;
}
// the *entire* stdout and stderr (buffered)
console.log(`stdout: ${stdout}`);
console.log(`stderr: ${stderr}`);
});
Hope it helps!
You can use child_process like so:
const { exec } = require("child_process");
var index2 = exec("./index2.js");

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.

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