Ukuqonda iimodyuli zeNode

Ibhlog

Ukuqonda iimodyuli zeNode

Ukuqala ngeemodyuli zeNode. Iimodyuli zingumbono obalulekileyo wokuqonda iiprojekthi zeNode.



Iimodyuli zingumbono obalulekileyo wokuqonda iiprojekthi zeNode. Kule posi, sigubungela iimodyuli zeNode: zifuna, ukuthunyelwa kwelinye ilizwe kunye nokungenisa elizweni elizayo.

Iimodyuli zeNode zikuvumela ukuba ubhale ikhowudi enokuphinda isebenze. Ungazidibanisa ngaphakathi kwenye. Usebenzisa iNode Package Manager (NPM), ungapapasha iimodyuli zakho kwaye uzenze zifumaneke eluntwini. Kwakhona, i-NPM ikwenza ukuba uphinde usebenzise iimodyuli ezenziwe ngabanye abaphuhlisi.



Kweli candelo, siza kugubungela indlela yokwenza iimodyuli zeNode kunye nelinye lezinto zalo:

  • Ifuna
  • Ukuthumela kwelinye ilizwe
  • Imodyuli. Ukuthumela ngaphandle

Sisebenzisa i-Node 10.x yemizekelo kunye ne-ES6 syntax. Nangona kunjalo, iikhonsepthi ziyasebenza kulo naluphi na uhlobo



require zisetyenziselwa ukutya iimodyuli. Ikuvumela ukuba ubandakanye iimodyuli kwiinkqubo zakho. Unokongeza ezakhelwe ngaphakathi iimodyuli zeNode.js, iimodyuli ezisekwe kuluntu (ii-node_modules) kunye neemodyuli zasekhaya.

Masithi sifuna ukufunda ifayile evela kwinkqubo yefayile. Node inemodyuli engundoqo ebizwa ngokuba yi-'fs ':

const fs = require('fs'); fs.readFile('./file.txt', 'utf-8', (err, data) => { if(err) { throw err; } console.log('data: ', data); });

Njengoko ubona, singenise imodyuli fs kwikhowudi yethu. Isivumela ukuba sisebenzise nawuphi na umsebenzi onamathele kuyo, njenge- readFile kunye nezinye ezininzi.

Guqula ukuntywila kwijavascript

Umsebenzi ofunekayo uya kujonga iifayile ngolu hlobo lulandelayo:

  1. Iimodyuli ezingundoqo zeNode.
  2. Iimodyuli kwifolda ye-node_modules.
  3. Ukuba igama lemodyuli line ./, / okanye ... /, liza kujonga umkhombandlela / ifayile kwindlela enikiweyo. Ihambelana nolwandiso lwefayile: * .js, * .json kunye * .node.

Sifunde ngendlela yokwenza iimodyuli zeNode.js kwaye sayisebenzisa kwikhowudi yethu. Iimodyuli zisivumela ukuba sisebenzise ikhowudi ngokulula. Banikezela ngokusebenza okuhlukanisiweyo kwezinye iimodyuli. Umsebenzi ofunekayo usetyenziselwa ukulayisha iimodyuli. Ukuthumela ngaphandle kunye nemodyuli.exports isivumela ukuba sichaze ukuba zeziphi iinxalenye zekhowudi yethu esifuna ukuyiveza. Siphonononge umahluko phakathi kwemodyuli.export kunye nokuthumela ngaphandle. Okokugqibela, sithathe ukukhetha okukhawulezayo malunga noko kuza kwiimodyuli zisebenzisa ukungenisa ngaphakathi.

# node-js