首页 > 解决方案 > 节点js需要执行功能

问题描述

我无法执行该功能。当我node app.js在终端运行时,我得到[Function: hi].

我有两个文件

应用程序.js

var show = require('./code');
console.log(show.hi);

代码.js

function hi() {
    console.log('I am inside function Hi');
}

module.exports.hi = hi;

如果我会做这样的事情,console.log(show.hi())那么我会得到

I am inside function Hi
undefined

标签: javascriptnode.js

解决方案


因为您没有调用该函数。

像这样称呼它

show.hi();

你得到 undefined 因为你的hi函数什么都不返回。删除console.log我的意思是只执行该功能show.hi()

总之console.log(show.hi);应该只是 show.hi()


推荐阅读