[英]Function is not defined error in nodejs. What's wrong with my code?
I created two files, app.js and helpers.js and when I tried to call a function from app.js, I got an error, function is not defined.我创建了两个文件,app.js 和 helpers.js,当我尝试从 app.js 调用 function 时,出现错误,function 未定义。
The two files are located in the same folder.这两个文件位于同一个文件夹中。 I think there is a problem the module.exports keyword,
我认为 module.exports 关键字有问题,
can anyone help me here?有人能帮我一下吗? Here is the code of the two separate files:
这是两个单独文件的代码:
//app.js const helpers= require('./helpers'); const total= sum(10,20); console.log("total:",total);
//helpers.js const sum = (a,b)=>{ return a+b; } module.exports={ sum };
And the error i am getting is:我得到的错误是:
const total= sum(10,20);
^
ReferenceError: sum is not defined
at Object.<anonymous> (E:\testing\app.js:5:14)
at Module._compile (internal/modules/cjs/loader.js:1147:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1167:10)
at Module.load (internal/modules/cjs/loader.js:996:32)
at Function.Module._load (internal/modules/cjs/loader.js:896:14)
at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:71:12)
at internal/main/run_main_module.js:17:47
Try this way:试试这个方法:
//app.js
const helpers= require('./helpers');
const total= helpers.sum(10,20);
console.log("total:",total);
//helpers.js
const sum = (a,b)=> {
return a+b;
}
module.exports = sum;
The problem is that you are exporting your function sum
in helpers.js
within an object:问题是您要在
helpers.js
中的 helpers.js 中导出 function sum
:
// helpers.js
// ...
module.exports = {
sum
};
If you want to access it from app.js
you can either use helpers.sum(...)
to access it:如果你想从
app.js
访问它,你可以使用helpers.sum(...)
来访问它:
// app.js
const helpers = require('./helpers');
const total = helpers.sum(10, 20);
console.log("total:", total);
... or you can use object deconstruction in the require
line: ...或者您可以在
require
行中使用 object 解构:
// app.js
const { sum } = require('./helpers');
// ...
Sure, the answer you posted does work, but from the name helpers.js
I assume that you might want to put multiple helper functions into that file.当然,您发布的答案确实有效,但是从名称
helpers.js
我假设您可能希望将多个帮助函数放入该文件中。 Then, you must use an object export - as mentioned above - in order to export all helper functions.然后,您必须使用 object 导出 - 如上所述 - 才能导出所有辅助函数。
You can try to edit the way to export the sum
function to:您可以尝试编辑将
sum
function 导出到的方式:
module.exports = sum;
That's because when you define:那是因为当你定义:
module.exports = {...}
it means you're exporting an object, not a function.这意味着您正在导出 object,而不是 function。
Usage:用法:
const sum= require('./helpers');
const total= sum(10,20);
console.log("total:",total);
Finally, I found a my answer.最后,我找到了我的答案。
I just needed to replace the helpers keyword with sum keyword in the first line of app.js and I also removed the curly braces around sum in helpers.js as per other's advice.我只需要在app.js的第一行中用sum关键字替换helpers关键字,并且根据其他人的建议,我还删除了helpers.js中sum周围的花括号。
The correct code is below正确的代码如下
//app.js
const sum= require('./helpers');
const total= sum(10,20);
console.log("total:",total);
and和
//helpers.js
const sum = (a,b)=>{
return a+b;
}
module.exports=sum;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.