నోడ్ JS పరిష్కరించడానికి ఎలా “SyntaxError: ఊహించని టోకెన్ దిగుమతి” జావాస్క్రిప్ట్ గుణకాలు ఉపయోగించి ఉన్నప్పుడు

ఎగుమతి గుణకాలు నోడ్ క్రమంలో మీరు ప్రతి ఫంక్షన్ పెట్టేందుకు, వస్తువు లేదా వేరియబుల్, ఆ ఎగుమతి అవసరం, modules.exports యొక్క లక్షణాలు వస్తువులా.

 

ఎగుమతి

In this example the module file is robot.js. Each function, వస్తువు లేదా మేము ఇతర ఫైళ్లు అందుబాటులో చేయాలనుకుంటున్న వేరియబుల్ మేము వాటిని ఎగుమతి వస్తువు నుండి లక్షణాలను చేయడానికి.

robot.js (మాడ్యూల్ ఫైలు)

exports.sayHello =

(msg) => console.log('Hello peeps!')


exports.hello = "హలో!"


exports.greetings =

  { ఇంగ్లీష్ : "హలో!",

    ఫ్రెంచ్ : "హలో!",

    spanish: "Ola!"

  }

 

Importing

నోడ్ దిగుమతి ప్రకటన మద్దతు లేదు, అందువలన పాత ఫంక్షన్ దిగుమతి ఉన్నప్పుడు ఉపయోగించవచ్చు అవసరం అవసరం.

ఇక్కడ మేము robot.js దిగుమతి (గమనిక: .JS ఐచ్ఛికంగా అవసరం ఫంక్షన్ లో వదిలి చేయవచ్చు) and assign it to an object that we can name anything. We name it robot. Our robot object now holds our exported modules from robot.js

ఈ క్రింది విధంగా మేము దానిని ఉపయోగించవచ్చు…

app.js (ప్రధాన ఫైలు)

కాన్స్ట్ రోబోట్ = అవసరం('./robot')


robot.sayHello()

console.log(robot.hello)

console.log(robot.greetings.french)

మీ మాడ్యూల్ వేరియబుల్ ఉపయోగించి మీరు మొదటి పంక్తిలో ఎంచుకున్నాడు ఆ వస్తువు పేరు ఉపయోగించడానికి గుర్తుంచుకో, ఫంక్షన్ లేదా వస్తువు.

గమనిక: Browsers do not support the require statement. Therefore you cannot use the require import module function with browsers without using a transpiler such as Babel or a bundler such as Browserify.

దొరకని ReferenceError - Opera వెబ్ బ్రౌజర్ v51 లో వివరించబడలేదు అవసరం
అవసరం జావాస్క్రిప్ట్ లో మాడ్యూల్ దిగుమతి ఫంక్షన్ వెబ్ బ్రౌజర్లు మద్దతు లేదు