Executing a script inside an ASAR archive - node.js

I am attempting to run a script that is archived inside an ASAR file like so:
var spawn = require('child_process').spawn;
var t = spawn('node', ['./bundle.asar/main.js'], {});
t.on('data', function(data){
console.log(data.toString());
});
t.stdout.pipe(process.stdout);
t.stderr.pipe(process.stderr);
FYI the above script is situated outside the ASAR archive.
However, all I get is the following error:
Cannot find module 'C:\Users\MyUser\tests\asar-test\bundle.asar\main.js'
The official docs on this particular issue are nonexistent.
Is there some way to either read the ASAR file or require a script inside it?
Thank you.

For posterity, the answer to this is to require your .js file like so:
var spawn = require('child_process').spawn;
var myScript = require('./bundle.asar/main.js');
var t = spawn('node', [myScript], {});
t.on('data', function(data){
console.log(data.toString());
});
t.stdout.pipe(process.stdout);
t.stderr.pipe(process.stderr);
Hope this helps.

Related

Error: spawn ENOENT on Windows

I'm on node v4.4.0 and on Windows 10. I'm using bunyan to log my node application.
try {
var fs = require('fs');
var path = require('path');
var spawn = require('child_process').spawn;
var through = require('through');
} catch (err) {
throw err;
}
var prettyStream = function () {
// get the binary directory of bunyan
var bin = path.resolve(path.dirname(require.resolve('bunyan')), '..', 'bin', 'bunyan');
console.log(bin); // this outputs C:\www\nodeapp\src\node_modules\bunyan\bin\bunyan, the file does exist
var stream = through(function write(data) {
this.queue(data);
}, function end() {
this.queue(null);
});
// check if bin var is not empty and that the directory exists
if (bin && fs.existsSync(bin)) {
var formatter = spawn(bin, ['-o', 'short'], {
stdio: [null, process.stdout, process.stderr]
});
// stream.pipe(formatter.stdin); // <- did this to debug
}
stream.pipe(process.stdout); // <- did this to debug
return stream;
}
The logging spits out in the console due to the fact I used stream.pipe(process.stdout);, i did this to debug the rest of the function.
I however receive the error:
Error: spawn C:\www\nodeapp\src\node_modules\bunyan\bin\bunyan ENOENT
at exports._errnoException (util.js:870:11)
at Process.ChildProcess._handle.onexit (internal/child_process.js:178:32)
at onErrorNT (internal/child_process.js:344:16)
at nextTickCallbackWith2Args (node.js:442:9)
at process._tickCallback (node.js:356:17)
at Function.Module.runMain (module.js:443:11)
at startup (node.js:139:18)
at node.js:968:3
I'm guessing this is a Windows error. Anyone have any ideas?
Use {shell: true} in the options of spawn
I was hit with this problem recently so decided to add my findings here. I finally found the simplest solution in the Node.js documentation. It explains that:
child_process.exec() runs with shell
child_process.execFile() runs without shell
child_process.spawn() runs without shell (by default)
This is actually why the exec and spawn behave differently. So to get all the shell commands and any executable files available in spawn, like in your regular shell, it's enough to run:
const { spawn } = require('child_process')
const myChildProc = spawn('my-command', ['my', 'args'], {shell: true})
or to have a universal statement for different operating systems you can use
const myChildProc = spawn('my-command', ['my', 'args'], {shell: process.platform == 'win32'})
Side notes:
It migh make sense to use such a universal statement even if one primairly uses a non-Windows system in order to achieve full interoperability
For full consistence of the Node.js child_process commands it would be helpful to have spawn (with shell) and spawnFile (without shell) to reflect exec and execFile and avoid this kind of confusions.
I got it. On Windows bunyan isn't recognized in the console as a program but as a command. So to invoke it the use of cmd was needed. I also had to install bunyan globally so that the console could access it.
if (!/^win/.test(process.platform)) { // linux
var sp = spawn('bunyan', ['-o', 'short'], {
stdio: [null, process.stdout, process.stderr]
});
} else { // windows
var sp = spawn('cmd', ['/s', '/c', 'bunyan', '-o', 'short'], {
stdio: [null, process.stdout, process.stderr]
});
}
I solved same problem using cross-spawn. It allows me to spawn command on both windows and mac os as one common command.
I think you'll find that it simply can't find 'bunyun', but if you appended '.exe' it would work. Without using the shell, it is looking for an exact filename match to run the file itself.
When you use the shell option, it goes through matching executable extensions and finds a match that way. So, you can save some overhead by just appended the executable extension of your binary.
I was having this same problem when trying to execute a program in the current working directory in Windows. I solved it by passing the options { shell: true, cwd: __dirname } in the spawn() call. Then everything worked, with every argument passed as an array (not attached to the program name being run).
I think, the path of bin or something could be wrong. ENOENT = [E]rror [NO] [ENT]ry

Spawning child_process in NodeJS with valid HOME folder set

I'm trying to spawn a process in NodeJS that accesses the my home folder, and can't see, to get either of the options below to work.
var spawn = require('child_process').spawn,
options = {stdio: 'inherit', env: process.env};
spawn('ls', ['~/'], options);
spawn('ls', ['$HOME'], options);
Output
ls: ~/: No such file or directory
ls: $HOME: No such file or directory
I've verified that options.env.HOME is properly set, any idea what I'm doing wrong?
Update
So this is what I ended up doing to make my use-case work (using script instead of ls):
spawn('script', [process.env.HOME], options);
Then, inside of my script:
#!/usr/bin/env bash
export HOME=$1
I still don't understand why options.env.HOME does not seem to work as expected.
process.env.HOME is what you want. Use it like so:
var spawn = require('child_process').spawn,
options = {stdio: 'inherit'};
var ls = spawn('ls', [process.env.HOME]);
ls.stdout.on('data', function(data){
console.log(String(data));
});
ls.stderr.on('data', function(data){
console.log(String(data));
});
Then you can set HOME in your shell when invoking the node script:
HOME='/tmp'; node ls.js
Alternatively, you don't have to overload HOME. To use whatever variable you like export it first, and then access it through process.env.:
export FOO='/tmp'; node ls.js

How to use prawn pdf inside a node / express app

In a node.js app I want to generate pdf docs and send it back to the user. I would like to use Prawn PDF as I have used it before and am comfortable using it.
I suppose I should use node's child_process.spawn to call a ruby script (that returns a pdf) to achieve this but I do not know how to actually implement it!
Am doing this:
spawn = require('child_process').spawn;
pdf = spawn('my_ruby_script');
Now how do I get hold of the returned pdf doc?
Thanks,
mano
I ended up with this eventually:
var spawn = require('child_process').spawn;
var child = spawn('ruby', ['print_pdf.rb', doc_id]);
var pdf = '';
child.on('data', function(data){
pdf += data;
});
child.on('exit', function(code){
if(code == 0){
res.setHeader('Content-Type', 'application/pdf');
res.send(pdf);
}
});
The ruby prawn script generates the pdf and at the end just 'puts' the rendered pdf which is available to child as 'data'.

Move files with node.js

Let's say I have a file "/tmp/sample.txt" and I want to move it to "/var/www/mysite/sample.txt" which is in a different volume.
How can i move the file in node.js?
I read that fs.rename only works inside the same volume and util.pump is already deprecated.
What is the proper way to do it? I read about stream.pipe, but I couldn't get it to work. A simple sample code would be very helpful.
Use the mv module:
var mv = require('mv');
mv('source', 'dest', function(err) {
// handle the error
});
If on Windows and don't have 'mv' module, can we do like
var fs = require("fs"),
source = fs.createReadStream("c:/sample.txt"),
destination = fs.createWriteStream("d:/sample.txt");
source.pipe(destination, { end: false });
source.on("end", function(){
fs.unlinkSync("C:/move.txt");
});
The mv module, like jbowes stated, is probably the right way to go, but you can use the child process API and use the built-in OS tools as an alternative. If you're in Linux use the "mv" command. If you're in Windows, use the "move" command.
var exec = require('child_process').exec;
exec('mv /temp/sample.txt /var/www/mysite/sample.txt',
function(err, stdout, stderr) {
// stdout is a string containing the output of the command.
});
You can also use spawn if exec doesn't work properly.
var spawn = require("child_process").spawn;
var child = spawn("mv", ["data.csv","./done/"]);
child.stdout.on("end", function () {
return next(null,"finished")
});
Hope this helps you out.

Spawned phantomjs process hanging

I'm trying to create a node server that spawns phantomjs processes to create screenshots. The grab.js script works fine when executed and I've confirmed that it writes to stdout. Problem is the node code that spawns the process simply hangs. I've confirmed that phantomjs is in the path. Anyone know what might be happening here or how I might troubleshoot this?
Here's the phantomjs code (grab.js) that renders the page and writes the data to stdout:
var page = require('webpage').create(),
system = require('system'),
fs = require('fs');
var url = system.args[1] || 'google.com';
page.viewportSize = {
width: 1024,
height: 1200
};
page.open(url, function() {
var b64 = page.renderBase64('png');
fs.write('/dev/stdout', b64, 'w');
phantom.exit();
});
And here's the node code that spawns the phantom progress and prints the result (hangs):
var http = require('http'),
exec = require('child_process').exec,
fs = require('fs');
exec('phantomjs grab.js google.com', function(error, stdout, stderr) {
console.log(error, stdout, stderr);
});
I have had similar issues with exec and then switched to using spawn instead and it worked.
According to this article , Use spawn when you want the child process to return huge binary data to Node, use exec when you want the child process to return simple status messages.
hth
I had same problem, in my case it was not in nodejs, but in phantomjs (v2.1).
It's known problem when phantom`s open method hangs.
Also, found second link (I guess same person wrote) in which author points that requestAnimationFrame is not working well with tweenJs, which causes freezing. PhantomJS returns unixtimestamp but tweenjs expects it to be DOMHighResTimeStamp, and so on...
Trick is to inject request-animation-frame.js (which is also provided in that article)

Resources