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

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)
})

Related

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.

How to generate angular application using child_processes.spawn() method in a specific directory?

I have recently started to study about node and I wanted the node server should use:
ng new my-app
To create application without manually typing in the terminal.
I found that we could run command using child_processes.spawn() or child_processes.exec() in node.
I cannot understand why am i not able to do so with the below code?
spawn("ng",[join("ng new ", folderName," --directory ", workspaceName)]);
I am new to this topic so I would require your help to understand this.
Try This
var spawn = require('child_process').spawn;
var child = spawn('npm install -g #angular/cli && cd your directory && ng new my-dream-app', {
shell: true
});
child.stderr.on('data', function (data) {
console.error("STDERR:", data.toString());
});
child.stdout.on('data', function (data) {
console.log("STDOUT:", data.toString());
});
child.on('exit', function (exitCode) {
console.log("Child exited with code: " + exitCode);
});

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

API HOST name from env variable node js

I have got a react app where I need to dynamically pass the API HOST name from environment (docker run --env API_HOST=localhost)
I'm using a child process in gulp to run 'node node.js'
//run npm install then node app
cp.spawn('npm install' ,{cwd:NODE_APP_FOLDER,env:process.env}, function(error, stdout, stderr) {
if (error) {
console.error(`exec error: ${error}`);
return;
}
var server = cp.spawn('node', ['app.js'], {
cwd: NODE_APP_FOLDER,
env: {
API_HOST:'localhost'
}
});
}
but in my code within the app process.env.API_HOST return undefined
Any help would be much appreciated

Executing grunt through node

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' }

Resources