Reputation: 33683
I'm new to nodejs. I have the following files and code:
// file: myfunc.js
function myfunc() { return "myfunc()"; }
exports = myfunc;
and
// file: index.js
var mf = require("./myfunc");
var mfunc = mf();
console.log(mfunc);
When I run node index.js
from command line, I get the error
var mfunc = mf()
^
TypeError: Object is not a function
Why do I get this error? I saw someone else's code which I paste below and I tried to follow the same approach of trying to get require()
to return a function instead of an object.
// file: index.js from another app
var express = require('express');
var app = express();
How come require('express')
can return a function but require('./myfunc')
can't return a function?
Upvotes: 1
Views: 226
Reputation: 106375
It should be...
module.exports = myfunc;
... instead. Quoting the doc:
If you want the root of your module's export to be a function (such as a constructor) or if you want to export a complete object in one assignment instead of building it one property at a time, assign it to
module.exports
instead ofexports
.
Upvotes: 2