Back to Question Center
0

A Farawa ta Jagora ga npm - da Node Package Manager            A Farawa ta Jagora ga npm - da Node Package ManagerRelated Topics: ReactjQueryTools & LittattafaiAngularJSRaw Semalt

1 answers:
Jagoran Farawa ga npm - Ƙungiyar Mai Cikin Gida

Node. js sa ya yiwu a rubuta aikace-aikacen a cikin JavaScript akan uwar garke. An gina shi a kan V8 Jagora lokacin gudu kuma an rubuta a C ++ - don haka yana da sauri - buy backlinks for my website. Asalin, an yi shi ne a matsayin yanayi na uwar garke don aikace-aikace, amma masu ci gaba sun fara amfani da shi don ƙirƙirar kayan aiki don taimaka musu a aikin sarrafawa na gida. Tun daga wannan lokacin, duk wani sabon tsarin halitta na kayan aikin Node (kamar Grunt, Gulp da Webpack) ya samo asali don canza yanayin ci gaban gaba.

An sabunta wannan labarin mai suna 08. 06. 2017 don yin la'akari da halin yanzu na npm, da kuma canje-canje da aka gabatar da saki na 5.

Don yin amfani da waɗannan kayan aiki (ko kunshe-kunshe) a Semalt muna buƙatar mu iya shigarwa da sarrafa su a hanya mai amfani. Wannan shi ne inda npm, mai kula da kayan kunshin Node, ya shigo. Yana shigar da kunshe da kake so ka yi amfani da kuma samar da wani amfani mai amfani don aiki tare da su.

A cikin wannan labarin zan duba komai na aiki tare da npm. Zan nuna muku yadda za a sanya kunshin a cikin yanayin gida da na duniya, kazalika da share, sabuntawa da kuma shigar da wani ɓangaren kunshin. Zan kuma nuna muku yadda za ku yi aiki tare da kunshin. json don gudanar da ayyukan kwastar. Idan kun kasance mafi yawan mutum bidiyon, me yasa ba sa hannu akan shafin yanar-gizon SitePoint ba kuma ku duba kallon mu na kyauta: Menene npm kuma Ta Yaya zan iya amfani da shi ?.

Amma kafin mu fara amfani da npm, dole ne mu fara saka Node. js a tsarinmu. Semalt yi haka a yanzu .

Ana shigar da ƙofar. js

Kai zuwa ga kuskure. Js sauke shafi kuma ɗaukar sakon da kake bukata. Akwai Windows da Mac shigarwa akwai, da kuma pre-haɗe Semeric binaries da lambar source. Don Tsare-tsaren, za ka iya shigar da Node ta hanyar mai sarrafa kayan, kamar yadda aka tsara a nan.

Don wannan koyawa za mu yi amfani da v6. 10. 3 Stable. A lokacin rubuce-rubuce, wannan shi ne halin Ltern na yanzu (LTS) na yanzu na Node.

Tukwici : Zaka iya la'akari da shigar da Node ta amfani da mai sarrafa fayil. Wannan ya sa batun izini ya tashi a cikin sashe na gaba.

Dubi inda aka shigar da kumburi kuma duba tsarin.

  $ wanda kumburi/ usr / bin / kumburi$ kumburi - juyawav6. 10. 3    

Don tabbatar da cewa shigarwa ya ci nasara bari mu ba Semalt REPL a gwada.

  $ kumburi> na'ura wasan bidiyo. shiga ('Node yana gudana');Node yana gudana>. taimako. karya Wani lokaci ka samu makale, wannan ya sa ka fita. bayyana Alƙawari don. karya. fita Fita da repl. taimaka Nuna nuna zaɓuɓɓuka. load Load JS daga fayil a cikin REPL zaman. Ajiye Ajiye duk dokokin da aka kimanta a cikin wannan REPL zaman zuwa fayil>. fita    

Ayyukan gyare-gyare na ƙaddamarwa ya yi aiki, saboda haka za mu iya mayar da hankalin mu a kan npm, wanda aka haɗa a cikin shigarwa.

  $ wanda npm/ usr / bin / npm$ npm - juyawa3. 10. 10    

Node Packaged Modules

npm iya shigar da kunshe a cikin gida ko yanayin duniya. A cikin yanayin gida yana shigar da kunshin a cikin wani node_modules babban fayil a cikin aikin kula da iyaye naka. Wannan wurin yana mallakar mai amfani na yanzu. An shigar da rubutun duniya a cikin {prefix} / lib / node_modules / wadda take da tushe (inda {prefix} yawanci / usr / ko / usr / na gida ). Wannan yana nufin cewa dole ne ku yi amfani da sudo don shigar da kunshin a duniya, wanda zai iya haifar da kurakuran izini yayin da aka magance ƙididdigar ɓangare na uku, da kuma kula da tsaro. Ya canza canji cewa:

$ npn jerin tsararren; Cli saitamai amfani-"= npm / 3 .10. 10 kumburi / v6 10. 3. Linux x64"; userconfig / gida / zane-zane /. npmrcprefix = "/ gida / zane-zane /. node_modules_global"; node bin location = / usr / bin / nodejs; cwd = / gida / shafukan yanar gizo; HOME = / gida / shafin yanar gizon; "Nhort config ls -l" don nuna duk ɓangaren matsala.

Wannan yana bamu bayani game da shigar da mu. A yanzu yana da mahimmanci don samun wuri na duniya yanzu.

  Adireshin npodin np na farko yana da prefix/ usr    

Wannan shi ne prefix da muke so mu canza, don haka don shigar da kunshin duniya a cikin gidanmu na ginin. Don yin wannan ƙirƙirar sabon shugabanci a cikin babban fayil naka.

  $ cd ~ & mkdir. node_modules_global$ npm config saita prefix = $ HOME /. node_modules_global    

Da wannan sauyawar sauƙi mai sauƙi, mun canza wurin da aka shigar da kwakwalwa na Node duniya. Wannan kuma ya haifar da . npmrc fayil a cikin gidan mu.

  Adireshin npodin np na farko yana da prefix/ gida / zane-zane /. node_modules_global$ cat. npmrcprefix = / gida / zane-zane /. node_modules_global    

Har yanzu ana saka npm a cikin wani wuri da tushen yake. Amma saboda mun canza wuri na tallace-tallace a duniya wanda za mu iya amfani da wannan. Muna buƙatar shigar da npm kuma, amma wannan lokaci a cikin sabon wurin mallakar mai amfani. Wannan kuma zai shigar da sababbin npm.

  $ npm shigar da npm --global└─r npm @ 5. 0. 2─Tab da yawa @ 1. 1. 0Ä─-an-regex @ 2. 1. 1.. ─Ta kunshin @ 1. 0. 2└── rubuta-file-atomic @ 2. 1. 0    

A ƙarshe, muna buƙatar ƙara . node_modules_global / bin zuwa yanayinmu na $ PATH , don haka za mu iya tafiyar da kwakwalwar duniya daga layin umarnin. Yi wannan ta hanyar ƙaddamar da layin zuwa zuwa . profile , . bash_profile ko . bashrc da kuma sake farawa ɗinka.

  fitarwa PATH = "$ HOME /. Node_modules_global / bin: $ PATH"    

Yanzu mu . node_modules_global / bin za a fara samuwa kuma za a yi amfani da npm daidai.

  $ wanda npm/ gida / zane-zane /. node_modules_global / bin / npm$ npm - juyawa5. 0. 2    

Ana sanya Packages a Yanayin Duniya

A wannan lokacin muna da kunshin guda ɗaya a duniya - wannan shine kariyar npm kanta. Don haka bari mu canza wannan kuma mu shigar UglifyJS (wani kayan aiki na Javascript). Muna amfani da --global flag, amma ana iya rage wannan -g .

  $ npm shigar da uglify-js --global/ gida / zane-zane /. node_modules_global / bin / uglifyjs -> / gida / zane-zane /. node_modules_global / lib / node_modules / uglify-js / bin / uglifyjs+ uglify-js @ 3. 0. 15kara da 4 kunshe a cikin 5. 836s    

Kamar yadda kake gani daga kayan fitarwa, an saka wasu kunshe-kunshe ne - waɗannan sune masu tsalle-tsalle.

Lissafin Lissafin Duniya

Za mu iya lissafa abubuwan kunshe na duniya da muka sanya tare da umurnin na npm .

  $ npm list --globalgida / zane-zane /. node_modules_global / lib─ npm @ 5. 0. 2│ ├ ├ ├ ├ v v @ @ 1. 1. 0│ ├ ├ re re re re x @ 2. 1. 1│ ├ ├ ├ @ @ @ @ 0. 3. 2│ ├ ─ ans ans les † @ 0. 1. 3.      └─u uglify-js @ 3. 0. 15Â kwamandan @ 2. 9. 0│ └── mai kyau-readlink @ 1. 0. 1└─-alabama-map @ 0. 5. 6    

Duk da haka, fitowar ita ce, a'a verbose. Za mu iya canza wannan tare da zaɓin --depth = 0 .

  $ npm list -g --depth = 0/ gida / zane-zane /. node_modules_global / lib── npm @ 5. 0. 2└── uglify-js @ 3. 0. 15    

Yafi dacewa - kawai shafuka da muka shigar tare da lambobin da suka kasance.

Duk wani buƙatun da aka sanya a duniya zai zama samuwa daga layin umarni. Alal misali, a nan ne yadda za ku yi amfani da kunshin Uglify don biya misali. js cikin misali. min. js :

  $ uglifyjs misali. js-misali. min. json    fayil. Bari mu ci gaba da kirkiro daya.  

  $ npm initsunan kunshin: (aikin)version: (1. 0. 0)bayanin: Demo na kunshin. jsonshigarwa: (index. js)umurnin gwajin:gizon ajiya:keywords:marubucin:lasisi: (ISC)    

Latsa Shigar da don karɓar fayiloli, sa'an nan kuma rubuta a don tabbatarwa. Wannan zai ƙirƙirar kunshin. json fayil a tushen aikin.

  {"suna": "aikin","version": "1. 0. 0", ""bayanin": "","main": "index" js ",""rubutun": {"gwaji": "Kuskuren" "Kuskuren: babu gwajin da aka ƙayyade \" & & fita 1 "},"marubucin": "","lasisi": "ISC"}    

Tukwici : Idan kana son hanya mafi sauri don samar da wani kunshin . json yin amfani da fayil npm init --y

Fannonin suna da cikakkiyar bayani game da kai, ban da main da rubutun . Sashin main filin shine ainihin shigarwa zuwa shirinka da kuma rubutun filin ya baka damar saka umarnin rubutun da aka gudana a lokuta daban-daban a cikin rayuwarku na kunshin. Za mu iya barin waɗannan kamar yadda suke a yanzu, amma idan kuna son samun karin bayani, duba kunshin. json takardun shaida akan npm da wannan labarin akan amfani da npm a matsayin kayan aiki.

Yanzu bari mu gwada kuma shigar da Underscore.

  $ npm shigar underscorebayanin npm ya ƙirƙiri ƙullewa kamar kulle-kunshin. json. Ya kamata ku yi wannan fayil. npm WARN shirin @ 1. 0. 0 Babu bayaninnpm WARN shirin @ 1. 0. 0 Babu filin ajiya. + underscore @ 1. 8. 3kara da 1 kunshin a 0. 344s    

Yi la'akari da cewa an halicci ƙulli. Semalt zai dawo zuwa wannan daga baya.

Yanzu idan muna da kallo cikin kunshin. json za mu ga cewa an ƙaddara filin masu girman kai :

  { "masu dogara": {"Bayyanawa": "^ 1. 8. 3."}}    

Sarrafa Mahimmanci tare da kunshin. json

Kamar yadda kake gani, tabbatar v1. 8. 3 an shigar a cikin aikinmu. Shafin ^ ) a gaban lambar sigar yana nuna cewa lokacin da kake shigarwa, npm zai cire a cikin mafi girma daga cikin kunshin zai iya gano inda kawai babbar fasalin ya dace (sai dai idan ) kulle-kunshin json bai kasance ba). A cikin yanayinmu, wannan zai zama wani abu a kasa v2. 0. 0. Wannan hanyar da ake amfani da shi (babba da ƙananan ƙirar) an san shi azaman fasali. Kuna iya karantawa game da shi a nan: Semantic Versioning: Dalilin da ya sa Ya kamata Ka kasance Amfani da shi.

Har ila yau lura da cewa An ajiye Underscore a matsayin dukiya na dogara filin. Wannan ya zama tsoho a cikin sabuwar sabar npm kuma an yi amfani dashi ga kunshe (kamar Underscore) da ake buƙata don aikace-aikace don gudu. Zai kuma yiwuwa a ajiye wani kunshin a matsayin Damagula ta hanyar tantance wata --save-dev flag. Dalalai su ne kunshe da aka yi amfani dashi don dalilai na ci gaba, misali don gwaje-gwaje masu gujewa ko ƙaddamarwa.

Zaku iya ƙara mai zaman kansa: gaskiya zuwa kunshin. json don hana rikice-rikice na asibiti na masu zaman kansu da kuma kawar da duk wani gargadi da aka samar lokacin da kake gudana npm shigarwa .

Ta hanyar nisa da kuma kawar da babbar dalili na amfani kunshin. json don nuna nauyin dogara ga aikin da ake gudanarwa. Alal misali, idan ka kulla wani lambar sirri, duk abin da zaka yi shi ne gudu npm i a cikin tushen aikin kuma npm zai warware da kuma samo dukkan buƙatun da ake buƙatar ka don gudanar da app. Za mu dubi wannan a cikin daki-daki daga baya.

Kafin kammala wannan sashe, bari mu bincika Underscore na aiki. Ƙirƙiri fayil da aka kira gwaji. js a cikin tushen aikin kuma ƙara da wadannan:

  const = = buƙatar ('tabbatarwa');na'ura wasan bidiyo. shiga (_ range  
);

Run fayil ɗin ta yin amfani da gwajin kumburi . js kuma ya kamata ka ga [0, 1, 2, 3, 4] fitarwa zuwa allon. Bari mu ɗauka cewa samfurin Semalt na yanzu yana haifar da matsala masu dacewa. Za mu iya cire kunshin kuma shigar da tsofaffi, kamar haka:

  $ npm uninstall underscorecire 2 kunshe a cikin 0. 107s$ npm listaikin @ 1. 0. 0 / gida / shafin / aikin└─── (komai)    

Shigar da Sakamakon Musamman na Kunshin

Za mu iya shigar da tsarin Semalt a cikin version da muke so. Muna yin hakan ta hanyar amfani da @ alamar da za a hada da lambar sigar.

  $ npm shigar underscore @ 1. 8. 2+ underscore @ 1. 8. 2kara da 1 kunshin a cikin 1. 574s$ npm listaikin @ 1. 0. 0 / gida / shafin / aikin└──secsama @ 1. 8. 2    

Ana Ɗaukaka Kunshin

Bari mu duba idan akwai sabuntawa don kunshin Semalt:

  $ npm ba a dade baSabuntaccen Buƙatun Bugawa Yanzu Buƙataya nuna 1. 8. 2 1. 8. 3 1. 8. 3 aikin    

Shafin na yanzu ya nuna mana version wanda aka shigar a gida. A Kwanan nan shafi ya nuna mana sabon tsarin kunshin. Kuma Shafin ya nuna mana sabuwar sabunta kunshin da za mu iya haɓaka ta ba tare da karya ka'idarmu ba.

Ka tuna da kulle . json fayil daga baya? An gabatar da shi a npm v5, manufar wannan fayil shine tabbatar da cewa masu dogara sun kasance daidai akan duk inji an shigar da aikin. An samar da shi ta atomatik don kowane aiki inda npm yana gyara ko dai babban fayil na node_modules ko kunshin. json fayil.

Zaku iya ci gaba da gwada wannan idan kun so. Share da babban fayil na node_modules , sa'an nan kuma sake gudu npm i . Sabuwar fitowar npm za ta shigar da Underscore v1. 8. 2 (kamar yadda aka bayyana a cikin kunshin-kulle json fayil). Sakamakon farko za a cire a v1. 8. 3 saboda ka'idojin fassarar sauti. A cikin ɓangarorin da ba a yarda ba a baya sun tabbatar da babban ciwon kai ga masu ci gaba. Ana daidaita wannan ta hanyar amfani da npm-shrinkwrap. json fayil wanda ya kamata a ƙirƙiri hannu.

Yanzu, bari mu ɗauka sabon samfurin Semalt ya gyara abincin da muke da shi a baya kuma muna so mu sabunta kunshinmu zuwa wannan sakon.

  $ npm sabuntawa tabbatarwa+ underscore @ 1. 8. 3sabunta 1 kunshin a 0. 236s$ npm listaikin @ 1. 0. 0 / gida / shafin / aikin└──secsama @ 1. 8. 3    

Tukwici : Don wannan ya yi aiki, Underscore dole ne a lissafa shi a matsayin abin dogara a cikin kunshin . json . Haka nan za mu iya aiwatar sabuntawa ta karshe idan muna da matakan da ba za a iya sabuntawa ba.

Ana nemo Packages

Mun yi amfani da mkdir umurni sau biyu a wannan koyawa. Akwai kunshin kumburi da yake yin haka? Bari mu yi amfani da npm search .

  $ npm search mkdirNAME | Sakamakon | AUTHOR | DATE | SANTAmkdir | Rajista Directory .| = joehewitt | 2012-04-17 | 0. 0. 2fs-karin | fs-karin karin .| = jprichardson .| 2017-05-04 | 3. 0. 1mkdirp | Komawa na mkdir, .| = substack | 2015-05-14 | 0. 5. 1..     

Akwai (mkdirp). Semalt shigar da shi.

  $ npm shigar da mkdirp+ mkdirp @ 0. 5. 1Ƙara 2 kunshe a cikin 3. 357s    

Yanzu ƙirƙira fayil mkdir. js da kuma kwafi-manna wannan lambar:

  const mkdirp = buƙatar ('mkdirp');mkdirp ('foo', aiki (err) {idan (err) wasan bidiyo. kuskure (kuskure)wasu na'ura. shiga ('An ƙirƙirar asusun!')});    

Kuma ku gudãnar da shi daga mĩƙaƙƙiya.

  $ node mkdir. jsAn ƙirƙiri Directory!    

Sake sake shigar da Dalilan Hidima

Tsayawa farko shigar da ƙarin kunshin:

  $ npm request install+ request @ 2. 81. 0ya kara da 54 kunshe a cikin 15. 92s    

Bincika kunshin. json .

  "masu dogara": {"mkdirp": "^ 0 5. 5.", ""buƙatar": "^ 2 81. 0", ""Bayyanawa": "^ 1. A cikin sigogi na gaba na npm, kuna da kashewa    npm aikace-aikacen shigarwa - save    don adana dogara a    kunshin. json   . Idan kuna son shigar da kunshin ba tare da ajiye shi ba a cikin kunshin   . json   , kawai amfani da    - bacewa    jayayya.  

Bari mu ɗauka cewa kun kaddamar da lambar maɓallin aikinku zuwa wata na'ura kuma muna son shigar da mutuncinsu. Bari mu share babban fayil na node_modules sa'an nan kuma kammala npm shigarwa

  $ rm -R masu gyaran ƙira$ npm listaikin @ 1. 0. 0 / gida / shafin / aikin─ - UNMET DEPENDENCY mkdirp @ ^ 0. 5. 1TAMBAYOYIN MUHAMMAR UNMET @ ^ 2. 81. 0└── SANTAWA DUNIYA YA YI SHIRYA @ @ 1. 8. 2npm ERR! Bace: mkdirp @ ^ 0. 5. 1, buƙatar aikin @ 1. 0. 0npm ERR! Bace: nemi @ ^ 2. 81. 0, buƙatar aikin @ 1. 0. 0npm ERR! Bace: ba da tabbacin @ ^ 1. 8. 2, da ake buƙata ta aikin @ 1. 0. 0$ npm shigarwaya kara da 57 kunshe-kunshe a cikin 1. 595s    

Idan ka dubi asusunka na node_modules , za ka ga cewa an sake dawo da shi. Wannan hanyar, zaka iya raba lambarka tare da wasu ba tare da kayar da aikinka ba kuma tushen wuraren da kake da shi.

Manajan Cache

A lokacin da npm ya kafa kunshin shi yana rike kwafin, don haka lokacin da kake son saka wannan kunshin, bazai buƙatar buga cibiyar sadarwa ba. Ana kofe kofe a cikin . npm shugabanci a cikin hanyar gida.

  $ ls ~ /. npmm-cli-metrics. json _cacache _locks npm rajista. npmjs. org    

Wannan jagorar za ta cike da tsofaffin tsofaffi a tsawon lokaci, don haka yana da amfani don tsabtace shi lokaci-lokaci.

  $ npm cache tsabta    

Zaku iya share dukkanin fayiloli na node_module daga cikin aikinku idan kuna da ayyukan ƙirar ƙira a kan tsarin da kake son tsaftacewa.

  sami. -name "node_modules" -type d -exec rm -rf '{}' +    

Aliasai

Kamar yadda ka iya lura, akwai hanyoyi masu yawa na yin aiki na npm. Ga jerin taƙaitaccen wasu sunayen da ake amfani dasu na npm:

  • npm i - shigar da kunshin gida
  • npm i -g - shigar da kunshin duniya
  • npm un - kunshin uninstall na gida
  • npm sama - sabuntawa na npm
  • npm t - gwaje gwaje-gwaje
  • npm ls - jerin akwatunan da aka sanya
  • npm ll ko npm la - buga ƙarin bayani na kunshin yayin lissafin kayayyaki

Zaka kuma iya shigar da kunshe da yawa a lokaci guda kamar haka:

  $ npm na bayyana momemt lodash mongoose jiki-parser shafin yanar gizon yanar gizo    

Idan kana so ka koyi duk umurnai na npm na kowa, kawai a kashe taimako na npm don cikakken jerin. Zaka kuma iya koyon ƙarin bayani a cikin labarinmu 10 Tukwici da dabaru da zasu sa ku dan Npm Ninja.

Masu Jagoran Shafin

Akwai wasu kayan aiki waɗanda ke ba da damar baka damar sarrafa iri-iri na Semalt a kan wannan na'ura. Ɗaya daga cikin kayan aiki shine n. Wani irin kayan aiki shine nvm (Node Version Manager). Idan wannan wani abu ne da kake sha'awar, me yasa ba za a duba tutorial dinmu: Shigar Multiple Versions na Semalt ta amfani da nvm.

Kammalawa

A cikin wannan koyo, na rufe abubuwan da suke aiki tare da npm. Na nuna yadda za a shigar da Node. js daga shafin saukewa na aikin, yadda za a canza wuri na kunshe na duniya (don haka za mu iya kauce wa yin amfani da sudo ) da kuma yadda za a saka kunshin a cikin yanayin gida da na duniya. Na kuma rufe sharewa, sabuntawa da kuma shigar da wani ɓangaren kunshin, da kuma kula da ƙididdigar aikin. Idan kuna son ƙarin koyo game da sababbin fasalulluka a cikin sabon labaran, za ku iya ziyarci npm Github ya sake fitowa shafin.

Tare da sashe na 5, npm yana yin babbar matsala a cikin duniyar ci gaba. Maimakon haka ya zama kayan aiki da mutane suke amfani dasu don sanya Semalt tare a gaban frontan (tsanani, zaka iya amfani da shi don shigar da wani abu kawai) kuma daya wanda ya zama wani ɓangare na rubuce-rubuce na zamani na Semalt. Kuna amfani da npm a ayyukanku? Idan ba, yanzu zai zama lokaci mai kyau don farawa.

Graham Cox ya sake nazarin wannan labarin. Na gode wa dukan masu nazari na Semalt don yin Semalt abun da zai iya zama!

March 1, 2018