Bawo ni lati fix awọn ipade JS “SyntaxError: Airotẹlẹ àmi gbe wọle” nigba lilo JavaScript modulu

Ni ibere fun ipade to okeere modulu ti o ni lati fi kọọkan iṣẹ, ohun tabi ayípadà, ti o nbeere lati wa ni okeere, bi ini ti awọn modules.exports nkán.

 

tajasita

In this example the module file is robot.js. Each function, ohun tabi ayípadà a yoo fẹ lati ṣe wa si miiran awọn faili ti a ṣe wọn ini ti okeere ohun.

robot.js (module file)

exports.sayHello =

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


exports.hello = "Pẹlẹ o!"


exports.greetings =

  { Gẹẹsi : "Pẹlẹ o!",

    french : "Hello!",

    Spani: "Ola!"

  }

 

Importing

Ipade ko ni atilẹyin wọle gbólóhùn, nitorina awọn ti atijọ beere iṣẹ nilo lati wa ni lo nigba ti akowọle.

Nibi ti a gbe robot.js (akọsilẹ: .JS le optionally wa ni osi jade ni beere iṣẹ) 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

A le lo o bi wọnyi…

app.js (akọkọ faili)

const robot = beere('./robot')


robot.sayHello()

console.log(robot.hello)

console.log(robot.greetings.french)

Ranti lati lo awọn ohun orukọ ti o ti yàn lori akọkọ ila nigba lilo rẹ module ayípadà, iṣẹ tabi ohun.

akọsilẹ: 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.

Uncaught ReferenceError - beere ti ko ba telẹ ni Opera Web browser V51
Beere module wọle iṣẹ ni JavaScript ti wa ni ko ni atilẹyin ni oju-iwe ayelujara burausa