[英]Imported variable in nodejs showing undefined
我是nodejs的新手。 嘗試一個簡單的導出(使用 module.export)並使用 require() 導入,但是當我嘗試打印出導入的常量時,我得到“未定義”。 請幫忙。 下面提供的代碼。 我也意識到有導出/導入 ES6 方式,但我想通過仍然使用 module.export 和 require() 來修復這個特定的錯誤。 IntelliJ IDEA 和 VScode 上的相同錯誤。
常量.js
const fname = 'Abdulraqib';
const lname = 'Olayanju';
module.export = {
fname : fname
};
index.js
const name = require('./constant');
console.log(name.fname);
package.json
{
"name": "fe-workshop",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"start": "node index.js",
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC"
}
它是module.exports
而不是module.export
const fname = 'Abdulraqib';
const lname = 'Olayanju';
module.exports = {
fname : fname
};
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.