Agar siz Node.js dasturchisi bo'lsangiz, npm va Yarn bilan tanishsiz. Hatto biridan ikkinchisidan foydalanish haqida kuchli fikrga ega bo'lishingiz mumkin. Ko'p yillar davomida ishlab chiquvchilar Node.js paket menejerlari, ayniqsa npm bilan ishlashda diskni saqlash va qurish vaqtida shishiradi.
Keyin, pnpm paketlar menejeri paydo bo'ldi, u paketlarni saqlashni boshqacha tarzda boshqaradi, bu foydalanuvchilarning joyini tejash va qurish vaqtini qisqartiradi. Mana pnpm farqni qanday tasvirlaydi :
“Paketni oʻrnatganingizda, biz uni kompyuteringizdagi global doʻkonda saqlaymiz, keyin nusxa koʻchirish oʻrniga undan qattiq havola yaratamiz. Modulning har bir versiyasi uchun diskda faqat bitta nusxa saqlanadi. Masalan, npm yoki ipdan foydalanganda, agar sizda lodash yordamida 100 ta paket bo'lsa, diskda 100 ta lodash nusxasi bo'ladi. pnpm sizga gigabayt disk maydonini tejash imkonini beradi!”
Pnpm tobora ommalashib borayotgani ajablanarli emas, chunki tobora ko'proq ishlab chiquvchilar uni o'zlari tanlagan paket menejeriga aylantirmoqdalar. O'sib borayotgan qabul qilish darajasi bilan bir qatorda, o'z ilovalarini Heroku-da ishlatadigan ko'plab ishlab chiquvchilar (men kabi) pnpm qo'llab-quvvatlanishini ko'rishni xohlashdi.
Yaxshiyamki, pnpm Node.js bilan tarqatiladigan Corepack orqali mavjud. Shunday qilib, 2024 yil may oyidan boshlab, pnpm endi Heroku-da mavjud !
Ushbu postda biz Heroku-da pnpm-ni boshlash uchun nima qilish kerakligini ko'rib chiqamiz. Bundan tashqari, biz undan foydalanishdan oladigan saqlash va qurish vaqtining baʼzi afzalliklarini koʻrsatamiz.
pnpm uzoq vaqtdan beri mavjud bo'lgan Node.js paket menejeri muammosini ortiqcha saqlash va qaramlikni boshqarishdagi samarasizliklarni hal qilish uchun yaratilgan. npm va Yarn bog'liqliklarni har bir loyihaning node_modules
ga nusxalaydi. Bundan farqli o'laroq, pnpm barcha loyihalar uchun barcha paketlarni yagona global do'konda saqlaydi va keyin ularni nusxalash o'rniga ushbu paketlarga qattiq havolalar yaratadi. Bu qanday ma'nono bildiradi?
Faraz qilaylik, bizda lodash
dan foydalanadigan Node.js loyihasi bor. Tabiiyki, loyiha node_modules
papkasiga ega bo'ladi, u lodash
deb nomlangan pastki papka bilan birga fayllar bilan to'ldirilgan. Aniqroq qilib aytadigan bo'lsak, [email protected]
da 639 ta fayl va fp
deb nomlangan boshqa pastki papkada yana 415 ta fayl mavjud.
Bu faqat lodash
uchun mingdan ortiq fayl!
Men oltita Node.js loyihasini yaratdim: ikkitasi pnpm, ikkitasi npm va ikkitasi Yarn bilan. Ularning har biri lodash
foydalanadi. Keling, lodash
bog'liqlik papkasidagi fayllardan faqat bittasi haqidagi ma'lumotlarni ko'rib chiqaylik.
~/six-projects$ ls -i npm-foo/node_modules/lodash/lodash.js 14754214 -rw-rw-r-- 544098 npm-foo/node_modules/lodash/lodash.js ~/six-projects$ ls -i npm-bar/node_modules/lodash/lodash.js 14757384 -rw-rw-r-- 544098 npm-bar/node_modules/lodash/lodash.js ~/six-projects$ ls -i yarn-foo/node_modules/lodash/lodash.js 14760047 -rw-r--r-- 544098 yarn-foo/node_modules/lodash/lodash.js ~/six-projects$ ls -i yarn-bar/node_modules/lodash/lodash.js 14762739 -rw-r--r-- 544098 yarn-bar/node_modules/lodash/lodash.js ~/six-projects$ ls -i pnpm-foo/node_modules/lodash/lodash.js 15922696 -rw-rw-r-- 544098 pnpm-foo/node_modules/lodash/lodash.js ~/six-projects$ ls -i pnpm-bar/node_modules/lodash/lodash.js 15922696 -rw-rw-r-- 544098 pnpm-bar/node_modules/lodash/lodash.js
lodash.js
faylining hajmi yarim megabaytdan bir oz ko'proq. Biz yumshoq havolalarni ko'rmayapmiz, shuning uchun birinchi qarashda har bir loyihada ushbu faylning o'ziga xos nusxasi borga o'xshaydi. Biroq, aslida bunday emas.
Men lodash.js
faylining inodesini ko'rsatish uchun -i
bayrog'i bilan ls
foydalandim. Siz pnpm-foo
va pnpm-bar
loyihalarida ko'rishingiz mumkin, ikkala fayl ham bir xil inodega ega ( 15922696
). Ular bir xil faylga ishora qilmoqdalar! Npm yoki Yarn uchun bunday emas.
Shunday qilib, agar sizda npm yoki Yarn dan foydalanadigan oʻnlab loyihalaringiz boʻlsa va bu loyihalar lodash
foydalansa, sizda lodash
ning oʻnlab turli nusxalari va oʻzlari lodash
foydalanadigan loyihalardagi boshqa bogʻliqliklar nusxalari boʻladi. Pnpm-da lodash
ushbu o'ziga xos versiyasini talab qiladigan har bir loyiha va qaramlik bir xil, yagona, global nusxaga ishora qiladi.
[email protected]
kodining hajmi 5 MBdan sal kam. Mashinangizda uning 100 ta ortiqcha nusxasini yoki faqat bitta global nusxasini olishni afzal ko'rasizmi?
Kun oxirida, pnpm bilan qaramlikni o'rnatish sezilarli darajada tezroq bo'lib, kamroq disk maydoni va kamroq resurslarni talab qiladi. Bir nechta loyihalar bo'ylab ishlaydigan yoki bulutli platformalarda bog'liqlikni boshqaradigan ishlab chiquvchilar uchun pnpm paketlarni boshqarishning sodda va tezroq usulini taklif qiladi. Bu pnpm-ni Heroku kabi soddalashtirilgan joylashtirish muhiti uchun ideal qiladi.
Uni ishlatishni boshlashga tayyormisiz? Keling, qanday qilib ko'rib chiqaylik.
Mashinamizda ishlayotgan Node.js versiyasi:
$ node --version v20.18.0
Yuqorida aytib o'tganimizdek, Corepack Node.js bilan birga keladi, shuning uchun biz pnpm-ni yoqish va ishlatish uchun corepack
foydalanishimiz kerak. Loyihamiz uchun papka yaratamiz. Keyin biz ushbu buyruqlarni bajaramiz:
~/project-pnpm$ corepack enable pnpm ~/project-pnpm$ corepack use pnpm@latest Installing [email protected] in the project... Already up to date Done in 494ms
Bu quyidagicha ko'rinadigan package.json
faylini yaratadi:
{ "packageManager": "[email protected]+sha512.22721b3a11f81661ae1ec68ce1a7b879425a1ca5b991c975b074ac220b187ce56c708fe5db69f4c962c989452eee76c82877f4ee80f474cebd61ee13461b6228" }
Bu pnpm-lock.yaml
faylini ham yaratadi.
Keyinchalik, loyihamizga bog'liqliklarni qo'shamiz. Namoyish maqsadida biz GitHub’dagi ushbu taqqoslash package.json
faylida topilgan dependencies
va devDependencies
ro‘yxatidan nusxa ko‘chirmoqdamiz. Endi bizning package.json
faylimiz quyidagicha ko'rinadi:
{ "version": "0.0.1", "dependencies": { "animate.less": "^2.2.0", "autoprefixer": "^10.4.17", "babel-core": "^6.26.3", "babel-eslint": "^10.1.0", ... "webpack-split-by-path": "^2.0.0", "whatwg-fetch": "^3.6.20" }, "devDependencies": { "nan-as": "^1.6.1" }, "packageManager": "[email protected]+sha512.22721b3a11f81661ae1ec68ce1a7b879425a1ca5b991c975b074ac220b187ce56c708fe5db69f4c962c989452eee76c82877f4ee80f474cebd61ee13461b6228" }
Keyin paketlarni o'rnatamiz.
~/project-pnpm$ pnpm install
Pnpm uchun foydalanish npm yoki ipga juda o'xshaydi va shuning uchun u intuitiv bo'lishi kerak. Quyida umumiy buyruqlar uchun turli xil foydalanishni taqqoslaydigan jadval mavjud ( ushbu postdan olingan).
Endi biz loyihani pnpm bilan qanday ishga tushirishni ko'rsatganimizdan so'ng (bu juda oddiy, to'g'rimi?), Biz Heroku-da ishlayotganda turli paket menejerlari uchun qurish vaqtlarini solishtirmoqchi edik. Biz bir xil bog'liqliklarga ega uchta loyihani o'rnatdik - npm, Yarn va pnpm yordamida.
Birinchidan, biz Heroku CLI ( heroku login
) ga kiramiz.
Keyin, biz loyiha uchun ilova yaratamiz. Biz npm loyihasi uchun qadamlarni ko'rsatamiz.
~/project-npm$ heroku apps:create --stack heroku-24 npm-timing Creating ⬢ npm-timing... done, stack is heroku-24 https://npm-timing-5d4e30a1c656.herokuapp.com/ | https://git.heroku.com/npm-timing.git
Biz loyihalarimiz uchun haqiqiy qurish vaqtlarini hisoblashimiz uchun Heroku jurnalidagi qurish bosqichlariga vaqt belgilarini qo'shadigan qurilish paketini topdik. Biz loyihamizga ushbu qurilish paketini qo'shmoqchimiz va uni Node.js uchun standart to'plamdan oldin ishga tushirmoqchimiz. Buni quyidagi ikkita buyruq bilan qilamiz:
~/project-npm$ heroku buildpacks:add \ --index=1 \ https://github.com/edmorley/heroku-buildpack-timestamps.git \ --app pnpm-timing ~/project-npm$ heroku buildpacks:add \ --index=2 heroku/nodejs \ --app npm-timing Buildpack added. Next release on npm-timing will use: 1. https://github.com/edmorley/heroku-buildpack-timestamps.git 2. heroku/nodejs Run git push heroku main to create a new release using these buildpacks.
Bo'ldi shu! Keyin biz npm tomonidan boshqariladigan loyihamiz uchun kodni ko'taramiz.
~/project-npm$ git push heroku main ... remote: Updated 4 paths from 5af8e67 remote: Compressing source files... done. remote: Building source: remote: remote: -----> Building on the Heroku-24 stack remote: -----> Using buildpacks: remote: 1. https://github.com/edmorley/heroku-buildpack-timestamps.git remote: 2. heroku/nodejs remote: -----> Timestamp app detected remote: -----> Node.js app detected ... remote: 2024-10-22 22:31:29 -----> Installing dependencies remote: 2024-10-22 22:31:29 Installing node modules remote: 2024-10-22 22:31:41 remote: 2024-10-22 22:31:41 added 1435 packages, and audited 1436 packages in 11s remote: 2024-10-22 22:31:41 remote: 2024-10-22 22:31:41 184 packages are looking for funding remote: 2024-10-22 22:31:41 run `npm fund` for details remote: 2024-10-22 22:31:41 remote: 2024-10-22 22:31:41 96 vulnerabilities (1 low, 38 moderate, 21 high, 36 critical) remote: 2024-10-22 22:31:41 remote: 2024-10-22 22:31:41 To address issues that do not require attention, run: remote: 2024-10-22 22:31:41 npm audit fix remote: 2024-10-22 22:31:41 remote: 2024-10-22 22:31:41 To address all issues possible (including breaking changes), run: remote: 2024-10-22 22:31:41 npm audit fix --force remote: 2024-10-22 22:31:41 remote: 2024-10-22 22:31:41 Some issues need review, and may require choosing remote: 2024-10-22 22:31:41 a different dependency. remote: 2024-10-22 22:31:41 remote: 2024-10-22 22:31:41 Run `npm audit` for details. remote: 2024-10-22 22:31:41 npm notice remote: 2024-10-22 22:31:41 npm notice New minor version of npm available! 10.8.2 -> 10.9.0 remote: 2024-10-22 22:31:41 npm notice Changelog: https://github.com/npm/cli/releases/tag/v10.9.0 remote: 2024-10-22 22:31:41 npm notice To update run: npm install -g [email protected] remote: 2024-10-22 22:31:41 npm notice remote: 2024-10-22 22:31:41 remote: 2024-10-22 22:31:41 -----> Build remote: 2024-10-22 22:31:41 remote: 2024-10-22 22:31:41 -----> Caching build remote: 2024-10-22 22:31:41 - npm cache remote: 2024-10-22 22:31:41 remote: 2024-10-22 22:31:41 -----> Pruning devDependencies remote: 2024-10-22 22:31:44 remote: 2024-10-22 22:31:44 up to date, audited 1435 packages in 4s remote: 2024-10-22 22:31:44 remote: 2024-10-22 22:31:44 184 packages are looking for funding remote: 2024-10-22 22:31:44 run `npm fund` for details remote: 2024-10-22 22:31:45 remote: 2024-10-22 22:31:45 96 vulnerabilities (1 low, 38 moderate, 21 high, 36 critical) remote: 2024-10-22 22:31:45 remote: 2024-10-22 22:31:45 To address issues that do not require attention, run: remote: 2024-10-22 22:31:45 npm audit fix remote: 2024-10-22 22:31:45 remote: 2024-10-22 22:31:45 To address all issues possible (including breaking changes), run: remote: 2024-10-22 22:31:45 npm audit fix --force remote: 2024-10-22 22:31:45 remote: 2024-10-22 22:31:45 Some issues need review, and may require choosing remote: 2024-10-22 22:31:45 a different dependency. remote: 2024-10-22 22:31:45 remote: 2024-10-22 22:31:45 Run `npm audit` for details. remote: 2024-10-22 22:31:45 npm notice remote: 2024-10-22 22:31:45 npm notice New minor version of npm available! 10.8.2 -> 10.9.0 remote: 2024-10-22 22:31:45 npm notice Changelog: https://github.com/npm/cli/releases/tag/v10.9.0 remote: 2024-10-22 22:31:45 npm notice To update run: npm install -g [email protected] remote: 2024-10-22 22:31:45 npm notice remote: 2024-10-22 22:31:45 remote: 2024-10-22 22:31:45 -----> Build succeeded! ...
Biz quyidagi qadamlar vaqtini ko'rib chiqdik, ya'ni Build succeeded
xabari oxirigacha:
Installing dependencies
Build
Pruning devDependencies
Caching build
Hammasi bo'lib, npm bilan bu qurish 16 soniya davom etdi.
Biz pnpm tomonidan boshqariladigan loyiha uchun xuddi shu sozlashni, shuningdek, timings buildpack yordamida amalga oshirdik.
~/project-pnpm$ heroku apps:create --stack heroku-24 pnpm-timing ~/project-pnpm$ heroku buildpacks:add \ --index=1 \ https://github.com/edmorley/heroku-buildpack-timestamps.git \ --app pnpm-timing ~/project-pnpm$ heroku buildpacks:add \ --index=2 heroku/nodejs \ --app pnpm-timing ~/project-pnpm$ git push heroku main … remote: 2024-10-22 22:38:34 -----> Installing dependencies remote: 2024-10-22 22:38:34 Running 'pnpm install' with pnpm-lock.yaml … remote: 2024-10-22 22:38:49 remote: 2024-10-22 22:38:49 dependencies: remote: 2024-10-22 22:38:49 + animate.less 2.2.0 remote: 2024-10-22 22:38:49 + autoprefixer 10.4.20 remote: 2024-10-22 22:38:49 + babel-core 6.26.3 … remote: 2024-10-22 22:38:51 -----> Build succeeded!
Pnpm bilan bir xil qurish uchun atigi 7 soniya vaqt ketdi.
Vaqtni tejash, biz aniqladikki, faqat dastlabki o'rnatish uchun emas. Qaramlik keshini ishlatadigan keyingi tuzilmalar ham pnpm bilan tezroq.
Node.js ishlanmasini birinchi marta boshlaganimda npm dan foydalanardim. Bir necha yil oldin men Yarn-ga o'tdim va men bundan foydalanardim. . . yaqingacha. Endi men pnpm ga o'tdim. Mahalliy kompyuterimda men katta disk maydonini bo'shata olaman. Qurilish ham tezroq. Va endi, pnpm uchun Heroku qo'llab-quvvatlashi bilan bu tsiklni yopadi, shunda men uni faqat mahalliy ishlab chiqishdan bulutda joylashtirishgacha ishlatishim mumkin.
Baxtli kodlash!