Kanjani ukulungisa Node JS “SyntaxError: yokungenisa Okungalindelekile ithokheni” uma usebenzisa i-JavaScript amamojuli

Ukuze engeyokuqala yokuthekelisa amamojuli une ukwabela umsebenzi ngamunye, into noma variable, okudinga ukuthi bathunyelwe, njengoba izindawo modules.exports uphikisana.

 

Ikhiphela

In this example the module file is robot.js. Each function, into noma variable singathanda ukuba atholakale amanye amafayela sibenza izakhiwo into ukuthekelisa.

robot.js (ifayela module)

exports.sayHello =

(uml) => console.log('Sawubona ukulunguza!')


exports.hello = "Sawubona!"


exports.greetings =

  { IsiZulu : "Sawubona!",

    French : "Sawubona!",

    Spanish: "Ola!"

  }

 

Importing

Node ayisekeli isitatimende yokungenisa, Ngakho elidala zidinga umsebenzi okumele kusetshenziswe uma ungenisa.

Lapha sesifaka robot.js (inothi: .JS kungaba optionally inyumbazana ku zidinga umsebenzi) 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

Singayisebenzisela kanje…

app.js (kufayela elikhulu)

const irobhothi = zidinga('./robot')


robot.sayHello()

console.log(robot.hello)

console.log(robot.greetings.french)

Khumbula ukusebenzisa igama into ukuthi ukhethe emgqeni wokuqala uma usebenzisa module yakho variable, umsebenzi noma into.

Inothi: 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 - zidinga akachaziwe e-Opera isiphequluli seWebhu v51

Require module import function in JavaScript is not supported in Web Browsers

Shiya impendulo