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");
Related
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) => {
// ...
});
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.
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);
});
Here is my problem, I want to create a CLI that automatically runs a test. Without the CLI, I'm able to run everything perfectly with the node command:
node test.js
Basically, I want to do the exact same thing as the command before, so I googled for a technique that does this. I found this:
#!/usr/bin/env node
'use strict';
const options = process.argv;
const { execFile } = require('child_process');
const child = execFile('node', ['../dist/test.js'], (error, stdout, stderr) => {
if (error) {
throw error;
}
console.log(stdout);
});
This method doesn't work for me because, in the test.js file, I'm using the ora package. And because this package is making real-time animations, it doesn't come in stdout.
Is there any way of executing in real time (without subprocess) my test.js using Node? I'm open to other methods, but I want to publish the CLI on NPM, so keep in mind that it has to be in JavaScript 😊.
You can find every file that I've talked here on GitHub. Normally, you wouldn't need this link, but I'm giving it to you if you need to have a closer look.
You should simply call your test() function from your CLI code, after requiring the module that defines it. Have a look at mocha and jasmine: you will see that while both tools provide a CLI, they also provide instructions for invoking the test frameworks from arbitrary JS code.
I can't think of a way without a sub-process. but this may help.
The child process exec will not work with the continuous output commands as it buffers the output the process will halt when that buffer is full.
The suitable solution is spwan :
var spwan = require('child_process').spwan
var child = spwan('node', ['../dist/test.js'])
child.stdout.on('data', function(data) {
console.log(data)
})
child.stderr.on('data', function(data) {
console.log(data)
})
Here is my solution, you can use the fs library to get the code of the file, and then, you simply use eval to execute in the same process.
const fs = require("fs");
function run(file) {
fs.readFile(file, (err, data) => {
eval(data.toString('utf8'))
})
}
I'm working on a nodejs application and I need to pipe a multi-line string into a shell command. I'm not a pro at shell scripting but if I run this command in my terminal it works just fine:
$((cat $filePath) | dayone new)
Here's what I've got for the nodejs side. The dayone command does work but there is nothing piped into it.
const cp = require('child_process');
const terminal = cp.spawn('bash');
var multiLineVariable = 'Multi\nline\nstring';
terminal.stdin.write('mul');
cp.exec('dayone new', (error, stdout, stderr) => {
console.log(error, stdout, stderr);
});
terminal.stdin.end();
Thanks for any help!
Here, you're starting up bash using spawn, but then you're using exec to start your dayone program. They are separate child processes and aren't connected in any way.
'cp' is just a reference to the child_process module, and spawn and exec are just two different ways of starting child processes.
You could use bash and write your dayone command to stdin in order to invoke dayone (as your snippet seems to be trying to do), or you could just invoke dayone directly with exec (bear in mind exec still runs the command in a shell):
var multiLineVariable = 'Multi\nline\nstring';
// get the child_process module
const cp = require('child_process');
// open a child process
var process = cp.exec('dayone new', (error, stdout, stderr) => {
console.log(error, stdout, stderr);
});
// write your multiline variable to the child process
process.stdin.write(multiLineVariable);
process.stdin.end();
With Readable Streams it's really easy to listen to the input
const chunks = [];
process.stdin.on('readable', () => {
const chunk = process.stdin.read()
chunks.push(chunk);
if (chunk !== null) {
const result = Buffer.concat(chunks);
console.log(result.toString());
}
});
With Writable Streams you can write to the stdout
process.stdout.write('Multi\nline\nstring');
Hopefully I could help you