નોડ જેએસ સુધારવા માટે કેવી રીતે “સિંટેક્સ ભૂલ: અનપેક્ષિત ટોકન આયાત” જ્યારે જાવાસ્ક્રિપ્ટ મોડ્યુલો મદદથી
નિકાસ મોડ્યુલ્સ નોડ ક્રમમાં તમે દરેક કાર્ય સોંપવા માટે હોય, પદાર્થ અથવા વેરિયેબલ, કે નિકાસ કરવાની જરૂર છે, modules.exports ગુણધર્મો પદાર્થ તરીકે.
નિકાસ
In this example the module file is robot.js. દરેક કાર્ય, પદાર્થ અથવા વેરિયેબલ અમે અન્ય ફાઇલોમાં ઉપલબ્ધ બનાવવા માંગો છો અમે તેમને નિકાસ પદાર્થ ગુણધર્મો બનાવવા.
robot.js (મોડ્યુલ ફાઇલ)
exports.sayHello = (MSG) => console.log('Hello peeps!') exports.hello = "હેલો!" exports.greetings = { ઇંગલિશ : "હેલો!", ફ્રેન્ચ : "હેલો!", સ્પેનિશ: "ઓલા!" }
Importing
નોડ આયાત નિવેદન સમર્થન કરતું નથી, તેથી જૂના જરૂરી કાર્ય જ્યારે આયાત વાપરી શકાય કરવાની જરૂર છે.
અહીં અમે robot.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 (મુખ્ય ફાઈલ)
const રોબોટ = જરૂરી('./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.

