简体   繁体   中英

Imported variable in nodejs showing undefined

I'm new to nodejs. trying out a simple export(using module.export) and import using require() but when i try to print out the imported constants i get "undefined". Please help. Code provided below. Also i realise there is the export/import ES6 way but i'd like to fix this particular error by still using module.export and require(). Same error on intelliJ IDEA and VScode.

constants.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"

}

It is module.exports not module.export

const fname = 'Abdulraqib';
const lname = 'Olayanju';

module.exports = {
    fname : fname
};

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM