Pablo Miranda
Pablo Miranda

Reputation: 369

Why I get process.stdin undefined

I'm trying to do a simple map with the following code (the only important part are the last 2 lines)

/**
 * Mapper chunk processing function.
 * Reads STDIN
*/
function process () {
var chunk = process.stdin.read(); // Read a chunk
if (chunk !== null) {
   // Replace all newlines and tab chars with spaces
   [ '\n', '\t'].forEach(function (char) {
       chunk = chunk.replace(new RegExp(char, 'g'), ' ');
   });

   // Split it
   var words = chunk.trim().split(' ');
   var counts = {};

   // Count words
   words.forEach(function (word) {
       word = word.trim();

       if (word.length) {
           if (!counts[word]) {
               counts[word] = 0;
           }

           counts[word]++;
       }
   });

   // Emit results
   Object.keys(counts).forEach(function (word) {
       var count = counts[word];
       process.stdout.write(word + '\t' + count + '\n');
   });
}
}
process.stdin.setEncoding('utf8');
process.stdin.on('readable', process); // Set STDIN processing handler

but i'm getting the following error:

process.stdin.setEncoding('utf8');
         ^
TypeError: Cannot read property 'setEncoding' of undefined

I simply can't understand the cause of it and I can't find anything on the internet about this. Why is my process.stdin undefined? any idea?

Upvotes: 2

Views: 2410

Answers (1)

djechlin
djechlin

Reputation: 60758

You shadowed the process module with your function named process.

I believe your best attempt at debugging this was to console.log(process) and see that it didn't look like what you expect.

Upvotes: 9

Related Questions