Olee otú idozi ọnụ JS “SyntaxError: Atụghị anya iriba mbubata” mgbe eji JavaScript modul
Ka ọnụ ka mbupụ modul i nwere na-ekenye onye ọ bụla ọrụ, ihe ma ọ bụ agbanwe, na-achọ ka a exported, ka Njirimara nke modules.exports jụrụ.
exporting
In this example the module file is robot.js. Onye ọ bụla ọrụ, ihe ma ọ bụ na agbanwe anyị ga-achọ ka dị ka ndị ọzọ faịlụ anyị na-eme ha Njirimara nke mbupụ ihe.
robot.js (modul file)
exports.sayHello = (msg) => console.log('Hello peeps!') exports.hello = "Nnọọ!" exports.greetings = { bekee : "Nnọọ!", french : "Hello!", spanish: "ola!" }
Importing
Ọnụ anaghị akwado ihe na nkwupụta, Ya mere ochie achọ ọrụ kwesịrị ka a na-eji mgbe importing.
N'ebe a, anyị mbubata robot.js (dee: .JS nwere ike optionally a na-na-achọ ọrụ) 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
Anyị nwere ike iji ya dị ka ndị a…
app.js (isi file)
const robot = achọ('./robot')
robot.sayHello()
console.log(robot.hello)
console.log(robot.greetings.french)
Cheta na-eji ihe aha na ị họọrọ ke akpa akara mgbe eji gị modul agbanwe, ọrụ ma ọ bụ ihe.
Note: 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.

