Let's go through this line by line because I see a lot of errors, both syntax and semantic.
foo.js
var misc = require('./sql_get');
console.log(misc.sql_file("on.sql"));
You defined in the function below sql_file to be asynchronous, so it does not return a value, but takes a callback that it passes the result to.
sql_util.js
fs = require('fs');
file = 'on.sql'
You have an unused module-global variable file. Remove this. It's going to cause confusion.
function sql_file(cb) {
var fileName = "./SQLs/" + sql_file;
sql_file is a function. I can tell because this code lives in a function called sql_file. Therefore, fileName will be "./SQLs/" + the .toString() result of the function, which is the source of the function. I think perhaps you want a parameter?
fs.readFile(fileName, function(err, buffer) {
if (err) return cb(err);
return cb(null, buffer.toString());
});
}
This seems ok.
sql_file(function(err, sqlstatement) {
if (err) throw err;
console.log('sql statement is: ' + sqlstatement);
});
};
I am not sure what you are trying to do here. Why are you calling the function?
module.exports.x = x;
module.exports.sql_fil = sql_file;
Both of these lines have a problem. There is no symbol defined named x. I am surprised this code runs without throwing an error on that line. Second, you are exporting sql_file as sql_fil, missing the letter e.
The Solution
Since what you asked about is parameter passing and you only have a single function, let's simplify this. You never want to use global variables. If a function needs a particular variable, just pass it as an argument.
foo.js
var misc = require('./sql_get');
misc.sql_file('on.sql', function (err, contents) {
console.log(contents);
});
sql_get.js (notice the file is renamed)
var fs = require('fs');
function sql_file(sqlFilename, cb) {
var fileName = "./SQLs/" + sqlFilename;
fs.readFile(fileName, function(err, buffer) {
if (err) return cb(err);
return cb(null, buffer.toString());
});
}
module.exports.sql_file = sql_file;