Can't write to a spawned child_process's stdin on Windows - node.js

The below code works fine on linux, but breaks on Windows 7
var spawn = require('child_process').spawn;
var sass = spawn('sass');
sass.stdout.on('data', function (data) {
console.log('' + data);
});
sass.stdin.write('.asdfsadf\n color: red', function () {
sass.stdin.end()
});
The error I get is
events.js:72
throw er; // Unhandled 'error' event
^
Error: This socket is closed.
at Socket._write (net.js:637:19)
at doWrite (_stream_writable.js:226:10)
at writeOrBuffer (_stream_writable.js:216:5)
at Socket.Writable.write (_stream_writable.js:183:11)
at Socket.write (net.js:615:40)
at Object.<anonymous> (e:\Projects\scaffold-angular\test.js:18:12)
at Module._compile (module.js:456:26)
at Object.Module._extensions..js (module.js:474:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
I've tried this with slimrb as well, same 'socket is closed' error.
The commands all work find when I manually enter it on the console (tried both MINGW32 and normal windows commandline).
$ sass
.asdfsadf
color: red
^Z
.asdfsadf {
color: red; }
My node is v0.10.28
edit: updated to v0.10.29, same issue :(

You can use the cross-spawn package as a drop-in replacement for child_process.spawn.

Related

Getting error while using "fs" module to read file in node.js

I am trying to read file using "fs" module in node.js as follows:
var fs=require("fs");
fs.read("E:/Node.js/readme.txt","utf8",function(err,data){
console.log(data);
});
But getting the following error:
fs.js:664
binding.read(fd, buffer, offset, length, position, req);
^
TypeError: fd must be a file descriptor
at Object.fs.read (fs.js:664:11)
at Object.<anonymous> (E:\Node.js\First.js:2:4)
at Module._compile (module.js:653:30)
at Object.Module._extensions..js (module.js:664:10)
at Module.load (module.js:566:32)
at tryModuleLoad (module.js:506:12)
at Function.Module._load (module.js:498:3)
at Function.Module.runMain (module.js:694:10)
at startup (bootstrap_node.js:204:16)
at bootstrap_node.js:625:3
Why is it so?
You should use readFile instead of read, read use for partial read bytes from file
fs.readFile("E:\\Node.js\\readme.txt",'utf8', function(err,data){
console.log(data);
});

How to reformat node.js fatal error message to one line?

I want to add nodejs error messages to a nodejs -> file.log -> hekad -> elasticsearch -> kibana stack and I'm facing the issue that heka apparently expects a single error message to be newline terminated, while for fatal errors it has multiple lines, e.g.:
test.js:5
a();
^
ReferenceError: a is not defined
at Object.<anonymous> (test.js:5:1)
at Module._compile (module.js:456:26)
at Object.Module._extensions..js (module.js:474:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Function.Module.runMain (module.js:497:10)
at startup (node.js:119:16)
at node.js:902:3
I'm trinking of adding code like to all my scripts:
process.stderr.oldwrite = process.stderr.write;
process.stderr.write = function (m) {
// so that stuff like EventEmitter memory leak gets to 1 line too
process.stderr.oldwrite(m.trim().replace(/\n/g, '\\n') + '\n');
};
process.on('uncaughtException', function(error) {
process.stderr.write(error.stack);
});
This way the output will be on one line clearly meaning it's about a single error, but I'm wondering if it's the correct approach to the problem?

Node.js FS module start prob

Ok so node.js has been driving me crazy. I have been trying to run the exact same code from the book. But it throwing me an error. The code is simple and watches a file for changes
const fs= require('fs');
fs.watch('target.txt', function() {
console.log("File 'target.txt' just changed!");
});
console.log("Now watching target.txt for changes");
the js file for the code and text.txt is in the same dir. And I get this error
$ node --harmony watcher.js
fs.js:1051
throw errnoException(process._errno, 'watch');
^
Error: watch ENOENT
at errnoException (fs.js:1019:11)
at FSWatcher.start (fs.js:1051:11)
at Object.fs.watch (fs.js:1076:11)
at Object.<anonymous> (C:\cygwin64\home\Sinan\try\watcher.js:5:5)
at Module._compile (module.js:456:26)
at Object.Module._extensions..js (module.js:474:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Function.Module.runMain (module.js:497:10)
at startup (node.js:119:16)
Thanks beforehand.
I would also appropriate an uptodate source for learning node, I have tried 2 books and I can not even run example codes
I removed .txt extension and it worked.
fs.watch('target', function () {
console.log("File target.txt just changed");
});

Node.js crashing when connecting to AWS S3

After migrating my application from IntoVPS to Digital Ocean, my application crashes when it tries to upload photos to AWS S3.
The differences between the servers are: IntoVPS was running Ubuntu 10.10, and Digital Ocean is running Debian; IntoVPS had node 0.8.x, and Digital Ocean has 0.10.26.
There are no firewalls on the new server that could be causing this issue (I've checked).
The crash errors are:
Error: read ECONNRESET
at errnoException (net.js:904:11)
at Pipe.onread (net.js:558:19)
Error: spawn ENOENT
at errnoException (child_process.js:988:11)
at Process.ChildProcess._handle.onexit (child_process.js:779:34)
Error: write EPIPE
at errnoException (net.js:904:11)
at Object.afterWrite (net.js:720:19)
Some of these might be related to forever trying to restart the process (I figured that's what spawn might mean). The one I'd focus on is the ECONNRESET one.
So, I'm running my process using forever, and I'm using the knox module to connect to S3.
After googling I found: https://github.com/LearnBoost/knox/issues/198
I tried adding res.resume() to the callback of putFile like it says in the, but nothing changes; I still get the ECONNRESET error.
I've spent an entire day (yesterday) trying to fix this issue, and I can't continue having my production application be broken, so I decided to try switching to an older version of Node in order to temporarily (but quickly) fix this issue. So, I installed the n module to try and get 0.8.26 as the node version installed. Unfortunately, n didn't work, and here's the issue I created on that: https://github.com/visionmedia/n/issues/170
EDIT:
After exiting my ssh session and opening a new one, n is working. However, switching to 0.8.26 version of node causes another errors:
/apps/Foobar/node_modules/dnode/node_modules/weak/node_modules/bindings/bindings.js:83
throw e
^
Error: Module version mismatch, refusing to load.
at Object.Module._extensions..node (module.js:485:11)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Module.require (module.js:362:17)
at require (module.js:378:17)
at bindings (/apps/Foobar/node_modules/dnode/node_modules/weak/node_modules/bindings/bindings.js:76:44)
at Object.<anonymous> (/apps/Foobar/node_modules/dnode/node_modules/weak/lib/weak.js:1:97)
at Module._compile (module.js:449:26)
at Object.Module._extensions..js (module.js:467:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Module.require (module.js:362:17)
at require (module.js:378:17)
at Object.<anonymous> (/apps/Foobar/node_modules/dnode/index.js:5:12)
at Module._compile (module.js:449:26)
at Object.Module._extensions..js (module.js:467:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Module.require (module.js:362:17)
at require (module.js:378:17)
at Object.<anonymous> (/apps/Foobar/controllers/sock.js:2:13)
at Module._compile (module.js:449:26)
at Object.Module._extensions..js (module.js:467:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Module.require (module.js:362:17)
at require (module.js:378:17)
at global.Controller (/apps/Foobar/globals.js:2:9)
at Object.<anonymous> (/apps/Foobar/controllers/generosity.js:209:12)
at Module._compile (module.js:449:26)
at Object.Module._extensions..js (module.js:467:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Module.require (module.js:362:17)
at require (module.js:378:17)
at global.Controller (/apps/Foobar/globals.js:2:9)
at Object.<anonymous> (/apps/Foobar/app.js:282:1)
at Module._compile (module.js:449:26)
at Object.Module._extensions..js (module.js:467:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Module.runMain (module.js:492:10)
at process.startup.processNextTick.process._tickCallback (node.js:245:9)
As you can see I'm having the darnedest time trying to fix this issue with S3, and I'm to the point of crying (metaphorically). I can't keep wasting time on this issue, but I can't seem to solve it. It's almost like Node itself is broken.
Can anyone provide any insight on what's going on? Any help with this is appreciated.
Why isn't knox working anymore?
EDIT
Here's more details.
Code that errors:
// S3
var endpoint = "https://s3.amazonaws.com/"+global.config.aws.s3.bucket+"/";
var knox = require('knox');
var s3 = knox.createClient(global.config.aws.s3);
var photosPrefix = 'photos/';
var model = module.exports;
model.emitter = new events.EventEmitter;
// ::Photos
model.createPhoto = function(info, cb){
if (!info.file || path.resolve(info.file) === path.resolve(global.__tempdir))
return cb(new Error('missing info.file'));
var db = info.db;
var photoId;
if (typeof info.public === 'undefined')
info.public = false;
// Move file to a specific location
var basename = path.basename(info.file);
var destination = photosPrefix+basename+'/original';
yarn
(function(){
// Upload photo to AWS
s3.putFile(
info.file,
destination,
{
'Content-Type': 'image/png'
},
this());
})
(function(err, res){
if (err) return this.error(new Error(err));
// Remove temporary file
fs.unlink(info.file, this());
})
(function(err){
if (err) return this.error(new Error(err));
// Retrieve photo stream from AWS
s3.getFile(destination, this());
})
(function(err, res){
if (err) return this.error(new Error(err));
// Get the dimensions of the photo using the stream (res)
gm(res).size(this());
})
(function(err, size){
if (err) return this.error(new Error(err));
// Insert record into photos table (include the dimensions, width and height)
db.query("INSERT photos (resource, width, height, public) VALUES (?, ?, ?, ?)", [basename, size.width, size.height, info.public], this());
})
(function(err, results){
if (err) return this.error(new Error(err));
photoId = results.insertId;
cb(null, photoId);
})
.error(function(err){
cb(err);
});
}
All photos no matter what the size or format cause the same error.
I found that the issue wasn't with AWS and knox, but rather with gm and the fact that GraphicsMagick wasn't installed on my system.
For reference for others, if you're experiencing similar errors, remember that gm doesn't report that GraphicsMagick or ImageMagick are absent when they are absent. I reported this error on the gm module here: https://github.com/aheckmann/gm/issues/273

limestone module in Nodejs throwing error with nodejs and sphinx

I have been trying to connect sphinx server with nodejs and limestone module. But it is throwing error as follows. Please help me on this.
node.js:134
throw e; // process.nextTick error, or 'error' event on first tick
^
Error: ENOENT, No such file or directory
at doConnect (net.js:549:5)
at Socket.connect (net.js:709:5)
at Object.createConnection (net.js:265:5)
at Object.connect (/home/node/node_modules/limestone/limestone.js:129:23)
at Object.<anonymous> (/home/node/www/bmchat-new/sphinx-connect.js:4:15)
at Module._compile (module.js:411:26)
at Object..js (module.js:417:10)
at Module.load (module.js:343:31)
at Function._load (module.js:302:12)
at Array.<anonymous> (module.js:430:10)
Source code of sphinx-connect.js
var limestone = require("limestone").SphinxClient(),
sys = require("sys");
limestone.connect("192.168.2.168:9312", // port. 9312 is standard Sphinx port. also 'host:port' allowed
function(err) { // callback
if (err) {
sys.puts('Connection error: ' + err);
}
sys.puts('Connected, sending query');
limestone.query(
{'query':'test', maxmatches:1},
function(err, answer) {
limestone.disconnect();
sys.puts("Extended search for 'test' yielded " +
answer.match_count + " results: " +
JSON.stringify(answer));
});
});
The issue is that i am using old limestone.js which i have got from npm(v1.0.106). So the updated one, you can get from github
Issue found is, old limestone.js using server_conn = tcp.createConnection(port); instead of the below one server_conn = tcp.createConnection(port, host);

Resources