Executing grunt through node - node.js

I'm building a node application that requires me to run grunt on a directory to build a "package". I'm using Node's exec command to execute grunt like so:
var exec = require('child_process').exec;
exec('grunt', function (error, stdout, stderr) {
if (error) {
console.log(error);
} else {
console.log('Grunt task completed');
}
});
Which triggers the below error and I'm not sure why...? (or what it means)
{ [Error: Command failed: /bin/sh -c grunt
] killed: false, code: 2, signal: null, cmd: '/bin/sh -c grunt' }
I'm running this on OSX but the application will be deployed to a server running Amazon Linux.
Update:
exec('/bin/sh -c grunt', function (error, stdout, stderr) {
if (error) console.log(error);
});
Outputs the below error (but works fine when ran from terminal):
{ [Error: Command failed: /bin/sh -c /bin/sh -c grunt
]
killed: false,
code: 2,
signal: null,
cmd: '/bin/sh -c /bin/sh -c grunt' }

Related

Command work in terminal but doesn't work with node.js

I try to unpack img file with 7z on Linux. In terminal it works fine:
7z x myimg.img
But when I try to do this from node.js script it fails:
const childProcess = require('child_process');
childProcess.exec('7z x myimg.img', (error, stdout, stderr) => {
if (error) {
console.error(`exec error: ${error}`);
return;
}
console.log(`stdout: ${stdout}`);
console.error(`stderr: ${stderr}`);
});
I get this message:
exec error: Error: Command failed: 7z x myimg.img
ERROR: myimg.img
Can not open the file as archive
If I run command whereis 7z I get the same path.

scheduled node script in crontab doesn't run after reboot

I've scheduled a node script in crontab to run after reboot by adding this line to my /etc/crontab:
#reboot /home/eugen/Documents/scripts/tests/run_after_boot/index.js
/home/eugen/Documents/scripts/tests/run_after_boot/index.js file contains this script, which is supposed to open vscode:
const { exec } = require('child_process');
exec('code', (err, stdout, stderr) => {
if (err) {
//some err occurred
console.error(err)
} else {
// the *entire* stdout and stderr (buffered)
console.log(`stdout: ${stdout}`);
console.log(`stderr: ${stderr}`);
}
});
After reboot, nothing happens. Also, I tried to add the same line (#reboot /home/eugen/Documents/scripts/tests/run_after_boot/index.js) by executing the crontab -e command in my terminal, but the same problem appeared: nothing happened after reboot.

Node.js child_process.exec "/bin/sh: 1: docker: not found" error

I created a micro-service using the "Moleculer" framework. I used child_process.exec for running docker commands in a shell. I get "/bin/sh: 1: docker: not found" error.
const { exec } = require("child_process");
.
.
.
exec("docker --version", (error, stdout, stderr) => {
if (error) {
console.error(`exec error: ${error}`);
return;
}
console.log(`stdout: ${stdout}`);
console.log(`stderr: ${stderr}`);
});
Tried giving absolute path as:
exec("/usr/bin/docker --version", (err...);
But still got the same error.
I am able to get the node version using:
exec("node --version", (err...);
I just had the same problem. The reason was simply that I had built a new image but the Docker client was not present in my container because I only did docker-compose restart <CONTAINER_NAME> and not docker-compose stop <CONTAINER_NAME>, docker-compose rm <CONTAINER_NAME> and docker-compose up -d <CONTAINER_NAME>.
https://stackoverflow.com/a/63281470/1707015

Run npm install programmatically in specified folder

I want to run npm install via typescript code in a specified directory.
I found this code:
npm.load({}, function(err: any) {
// handle errors
// install module ffi
npm.commands.install(["hello-world#0.0.1"], function(err: any, data: any) {
// log errors or data
});
npm.on('log', function(message: any) {
// log installation progress
console.log(message);
});
});
But now I don't want to install hello-world, but just run npm install (without any package).
Additionally it should run in a path that I can specify, like ./folder/subfolder
How can I do that?
Apart from exec it's also possible to use the npm package:
import * as cp from 'child_process';
var npm = process.platform === 'win32' ? 'npm.cmd' : 'npm';
var path = '/path_to_npm_install';
const result = cp.spawnSync( npm, ['install'], {
cwd: path
});
If you're using Nodejs, which I think you are, you can run
child_process.exec('npm install') // or any other command which you give from terminal or command prompt
Check the documentation for child_process
https://nodejs.org/api/child_process.html#child_process_child_process_exec_command_options_callback
You can create a nodejs script that expect the directory path from user and create a child process and execute that command in that.
index.js
const { exec } = require('child_process');
exec(`cd /${process.env.PATH} | npm install`, (error, stdout, stderr) => {
if (error) {
console.error(`exec error: ${error}`);
return;
}
console.log(`stdout: ${stdout}`);
console.log(`stderr: ${stderr}`);
});
PATH=/path_of_directory_to_run_npm_install node index.js
Read more about child_process from nodejs documentation - https://nodejs.org/api/child_process.html#child_process_child_process_exec_command_options_callback

How I can create build file with Node.JS script?

How I can do it with Node.js(script file not in cmd consol):
go to the folder with the project
do npm i
do webpack -p
?
Create a bash script with all you want to do:
yourbash.sh:
cd /yourdirectory
npm i
webpack -p
then just spawn this process in your node
const spawn = require('child_process').exec
spawn('sh yourbash.sh', [], function (err, stdout, stderr) {
if (err) { console.log(err) } // handle error
// standard output from the bash script
console.log(stdout)
})
Alternatively, you can skip the bash file entirely by doing this:
const spawn = require('child_process').exec
spawn('cd /yourdirectory && npm i && webpack -p', [], function (err, stdout, stderr) {
if (err) { console.log(err) } // handle error
// standard output from the bash script
console.log(stdout)
})

Resources