Sidee si uu u xaliyo JS Noode ah “SyntaxError: dejinta calaamo lama filaan ah” marka la isticmaalayo modules JavaScript

Si Guntin in modules dhoofinta aad leedahay inuu ku meeleeyo shaqo kasta, shayga ama variable, oo u baahan in la dhoofiyo, sida guryaha ee modules.exports u diidid.

 

dhoofinta

In this example the module file is robot.js. Each function, shayga ama variable waxaan jeclaan lahaa in la sameeyo diyaar u ah files kale waxaan iyaga ka dhigi sifooyinka wax dhoofinta.

robot.js (file module)

exports.sayHello =

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


exports.hello = "Hello!"


exports.greetings =

  { Ingiriiska : "Hello!",

    Faransiiska : "Hello!",

    Isbaanish: "Ola!"

  }

 

Keenista

Guntin ma taageeri war dejinteeda, Sidaa darteed jir ah u baahan tahay shaqo u baahan in la isticmaalo marka loo dhoofiyo.

Halkan waxaan ku soo dhoofsadaan robot.js (note: .JS optionally waa laga tegi karaa in shaqada ay u baahan yihiin) 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

Waxaan u isticmaali karaa sida soo socota…

app.js (file ugu weyn)

= Robot const u baahan('./robot')


robot.sayHello()

console.log(robot.hello)

console.log(robot.greetings.french)

Xusuusnow in la isticmaalo magaca shayga idiin doortay on line ugu horeysay ee marka la isticmaalayo aad variable module, shaqo ama wax.

Ogow: 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 Uncaught - waxay u baahan yihiin aan la qeexay in Opera v51 Web browser
Waxay u baahan yihiin module function dejinta ee JavaScript ma aha oo ay taageerayaan in Web daalacashaduna