在node.js中导出函数但是有错误TypeError:xxx不是函数

时间:2018-02-15 18:08:53

标签: node.js

我在node.js中有两个文件,一个是hello.js,另一个文件将调用hello.js中的函数。我试图导出函数 - hello_2018,但它出现错误说:

TypeError: hello.hello_2018 is not a function

这是代码。

hello.js

module.exports =  (app) => {

  function hello_2018(){
    console.log('hello you');
  }

  hello_2018:hello_2018

};

第二节课

var hello = require('./hello');
console.log(hello);
hello.hello_2018();

1 个答案:

答案 0 :(得分:1)

在导出功能中,您正在编写键值对。

function hello_2018() {
    console.log('hello you');
}
module.exports = {
    hello_2018: hello_2018
};
// Second file
var hello = require('./hello');
console.log(hello);
hello.hello_2018();