nodejs require语句问题

nodejs require statement issue

我是 nodejs 新手。我有以下文件和代码:

// file: myfunc.js
function myfunc() { return "myfunc()"; }
exports = myfunc;

// file: index.js
var mf = require("./myfunc");
var mfunc = mf();
console.log(mfunc);

当我从命令行 运行 node index.js 时,出现错误

var mfunc = mf()
            ^
TypeError: Object is not a function

为什么会出现此错误?我看到别人的代码并粘贴在下面,我尝试采用相同的方法尝试让 require() 到 return 一个函数而不是一个对象。

// file: index.js from another app
var express = require('express');
var app = express();

为什么require('express')可以return一个函数,而require('./myfunc')不能return一个函数?

应该是...

module.exports = myfunc;

... 相反。引用 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 of exports.