I am trying to call an exported function inside the nodejs module.
exports.sayHello = function (msg) {
console.log(msg)
}
function run(msg) {
this.sayHello(msg);
}
run("hello");
when I run this script I got TypeError: this.sayHello is not a function
Simply declare it separately from exporting it (and don't use this
when calling it, you haven't attached it to an object):
function sayHello(msg) {
console.log(msg)
}
exports.sayHello = sayHello;
function run(msg) {
sayHello(msg);
}
run("hello");
That said, you could call it via exports
:
exports.sayHello = function (msg) {
console.log(msg)
}
function run(msg) {
exports.sayHello(msg); // <===
}
run("hello");
...but that seems a bit roundabout to me, though I'm told it can help with testing, such as in this example .
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.