Issue with function scope - function is undefined

I am working in an app built with NodeJS. I am using modules, here is my code so far:

CODE: PROGRAM.JS

var path     = process.argv[2];
var ext      = '.' + process.argv[3];
var prsFiles = require('./fpmod');

function callback(err, files) {
    if(!err) {
        success(ext, files);
    } else {
        throw ('uhoh there was an error! :/');
    }
}

prsFiles.process(path, ext, callback);

CODE: FPMOD.JS

var fs      = require('fs');
var pathMod = require('path');
var i       = 0;

function success(ext, files) {
    for(i; i < files.length; i++) {
        // console.log('File comes in as: ' + files[i] + ' and we\'re filtering on .' + ext);
        var currentFile    = files[i];
        var currentFileExt = pathMod.extname(currentFile);
        // console.log('grabbed ext is: ' + currentFileExt);

        if(ext) {
            if(ext === currentFileExt) {
                console.log(currentFile);
            }
        } else {
            console.log(currentFile);
        }
    }
}

function processFiles(path, ext, callback) {
    var success = success;
    fs.readdir(path, callback);
}

exports.process = processFiles;

The error I am getting says that the success function is not defined. Why is that? If my understanding of function scopes is correct, it should have access to the success function! What am I getting wrong?

Scoping in JavaScript is lexical. What matters is the set of symbols visible to code in a function at the time the function is defined. I guess it's more accurate to say that it's the set of scopes at the point of definition that matters. Function scopes are (excepting delete, which is weird and disallowed in strict mode anyway) immutable in terms of the set of symbols they export, but the global scope isn't. Module scope works like function scope (I think; I admit to not being a Node expert.) However the global scope isn't really in question here because you're working with modules.

Your "callback" function, defined in your main module, has access to no symbol called "success" at the point that it's defined. The fact that the function in the other module has a local symbol called "success" cannot help - that symbol is completely alien to the "callback" function. That's just not something you can do in JavaScript.

Remember that in the Node environment, unexported symbols in a module are effectively private to that module.

You are not exporting the success function.

You are expecting it to be present in the first snippet, even if you didn't do anything to make it available. You could do something like this:

module.exports = {
  processFiles: processFiles,
  success: success
};

and then use it

var prsFiles = require('./fpmod');

function callback(err, files) {
  if(!err) {
    prsFiles.success(ext, files);
  } else {
    throw ('uhoh there was an error! :/');
  }
}