I am currently using axios through npm import axios from 'axios'
Is there o possibility to console.log the axios version after the whole application has been built?
You could execute a bash command from nodejs to get an output of npm ls for axios.
const { exec } = require('child_process');
exec('npm ls | grep axios', (err, stdout, stderr) => {
if (err) {
return;
}
console.log(`Axios package: ${stdout}`);
console.log(`stderr: ${stderr}`);
});
Related
I'm trying to commit and push changes using node without using any external npm packages
const { spawn,exec } = require('child_process');
const fs = require('fs');
const { stderr } = require('process');
(async ()=>{
if(fs.existsSync('./repo_dir'))
fs.rmSync('./repo_dir',{recursive:true});
exec(`git config --global --replace-all user.name "NAMEHERE" && git config --global --replace-all user.email "EMAIL#gmail.com" && git config --list`,(err,stdout,stderr)=>{
if(err)
console.log(`error: ${err}`);
if(stderr)
console.log(`stderr: ${stderr}`);
console.log(`stdout: ${stdout}`);
let git_spawn=spawn(`git`,[`clone`,`https://username:token#github.com/username/repo_dir.git`]);
git_spawn.stdout.on("data", data => {
console.log(`cloning stdout: ${data}`);
});
git_spawn.stderr.on("data", data => {
console.log(`cloning stderr: ${data}`);
});
git_spawn.on('error', (error) => {
console.log(`cloning error: ${error.message}`);
});
git_spawn.on("close", code => {
console.log(`child process exited with code ${code}`);
exec(`cd ./git_repo && touch blank.js && git add -A && git commit -m "updating" && git push && cd ..`,(err,stdout,stderr)=>{ //error somewhere here
console.log(`pushing error: ${err}`);
console.log(`pushing stdout: ${stdout}`);
console.log(`pushing stderr: ${stderr}`);
});
});
})
})();
but for some reason it doesn't work and I can't even pin point the error
output:
stdout: cloning stderr: Cloning into 'repo_dir'...
child process exited with code 0 pushing error: Error: Command failed:
cd ./repo_dir && touch blank.js && git add -A && git commit -m
"updating" && git push && cd ..
pushing stdout: On branch main Your branch is up to date with
'origin/main'.
nothing to commit, working tree clean
pushing stderr:
I'm guessing the problem is somewhere in committing and pushing
I have a jar file that returns a json. How can I run it and take the output through javascript?
There is a no browser side java compiler i guess, so you must use javascript on the backend side, i suggest use nodejs
const { exec } = require('child_process');
exec('java -jar /file/to/path.jar',(error, stdout, stderr) => {
console.log(stdout);
console.log(stderr);
if (error !== null) {
console.log(`exec error: ${error}`);
}
});
I want to exectue following Shell Command with the Execute Option form Javascript:
"sed -i 4r<(sed '1,5!d' /etc/wpa_supplicant/wpa_template.template) /etc/wpa_supplicant/wpa_supplicant.conf"
Then i try this command in the shell console it works perfektly. But after starting it with the .js programm nothing is happen. I just want to copy the first 5 lines in template and add them to the config file.
You can do with exec from child_process
const exec = require('child_process').exec
exec("sed -i 4r<(sed '1,5!d' /etc/wpa_supplicant/wpa_template.template) /etc/wpa_supplicant/wpa_supplicant.conf", (err, stdout, stderr) => {
if (err) {
return console.error(err)
}
console.log(`stdout: ${stdout}`)
console.log(`stderr: ${stderr}`)
})
You can use the below code to execute any shell command and replace the o/p to a new file(create a new file) everytime you run it. And, you should use the line-reader module to get the line from the files.
npm install --save line-reader
const fs = require('fs');
const lineReader = require('line-reader');
const { exec } = require('child_process');
const cmd = "sed -i 4r<(sed '1,5!d' /etc/wpa_supplicant/wpa_template.template) /etc/wpa_supplicant/wpa_supplicant.conf"
exec(cmd, (err, stdout, stderr) => {
if (err) {
//some err occurred
console.error(err)
} else {
// the *entire* stdout and stderr (buffered)
fs.writeFile('file path', stdout, (err) => {
// throws an error, you could also catch it here
if (err) throw err;
// success case, the file was saved
console.log('file saved');
readFile1();
});
}
});
const readFile1 = () => lineReader.eachLine('file path', function(line) {
//make logic to take number of lines you want to take.
console.log(line);
});
I'm trying to open application using nodejs (I'd like this process to be workable in cross-platform , MAC/Windows ..etc
here's the code
const { exec } = require('child_process')
const fs = require('fs')
// giving the required permissions , not really sure if 777 is the right choice
fs.chmodSync(appPath,777);
exec("open -a " + path , (err, stdout, stderr) => {
console.log(err);
console.log(stdout);
console.log(stderr);
});
console.log(err); // logs this error
{ Error: Command failed: open -a /location.app
LSOpenURLsWithRole() failed for the application /location.app with error -10810
when executing the same command in terminal (out of node) , it fires the same issue
How can I run an installer of an independent program (exe file) from inside of my code?
The idea in general is program which install other programs.
Thanks!
Barak
use exec of node.js
const { exec } = require('child_process');
exec('path/to/exe', (error, stdout, stderr) => {
if (error) {
console.error(`exec error: ${error}`);
return;
}
console.log(`stdout: ${stdout}`);
console.log(`stderr: ${stderr}`);
});