Save 37% off PRO during our Black Friday Sale! »

Corepack ~Node.jsに追加されたパッケージマネージャーマネージャー~ / #tng37

Corepack ~Node.jsに追加されたパッケージマネージャーマネージャー~ / #tng37

Node学園 37時限目で話しました。

Node学園 37時限目 オンライン - connpass
https://nodejs.connpass.com/event/221358/

参考資料: https://zenn.dev/teppeis/articles/2021-05-corepack

5cf7e9533a457726cd51232e06c1da9a?s=128

Masashi Hirano

August 30, 2021
Tweet

Transcript

  1. Corepack NodeֶԂ 37࣌ݶ໨ @shisama_ Node.jsʹ௥Ճ͞ΕΔύοέʔδϚωʔδϟʔϚωʔδϟʔͷ࿩

  2. ฏ໺ ণ࢜ / Masashi Hirano @shisama_ shisama Node.js Core Collaborator

  3. CorepackΛNode.jsʹ௥Ճ͢ΔPR͕Ϛʔδ🎉 https://github.com/nodejs/node/pull/39608

  4. Corepackͱ͸ʁ🤔

  5. https://github.com/nodejs/corepack/blob/main/README.md Nodejs͕։ൃɾ؅ཧ͍ͯ͠Δπʔϧ

  6. Corepackͱ͸ • ύοέʔδϚωʔδʔΛ؅ཧ͢Δπʔϧ • ࠷ॳ͸pmm(package manager manager)ͱ͍͏໊લͩͬͨ • Node.jsΛΠϯετʔϧ͢Δ͚ͩͰɺYarn ͱ

    pnpm Λ࢖͑ΔΑ͏ʹ͢ Δͷ͕Ϟνϕʔγϣϯ • YarnͷϦʔυϝϯςφʔ͕ओʹ։ൃ͍ͯ͠Δ
  7. ͳͥ࡞ΒΕͨʁ🤔

  8. ݱࡏͷύοέʔδϚωʔδϟʔʹؔ͢Δ໰୊఺ • npmҎ֎ͷύοέʔδϚωʔδϟʔ͸Ϣʔβʔ͕ผ్Πϯετʔϧ͠ ͳ͚Ε͹͍͚ͳ͍ɻ 
 ⚠ Node.jsͷόʔδϣϯΛมߋ͢ΔͨͼʹΠϯετʔϧ͕ඞཁͰखؒ • npm Ҏ֎ͷύοέʔδϚωʔδϟ͕༏Ε͍ͯͯ΋ɺٕज़తͳཁૉΑ

    ΓΠϯετʔϧͷखؒͱ͍ͬͨ୯७͕͞༏ઌ͞Εͯ͠·͏͜ͱ͕͋Δ • ΤίγεςϜ΁ͷӨڹྗ͕σϑΥϧτͷnpmʹूத͍ͯ͠Δ
  9. ໰୊ΛͲ͏΍ͬͯղܾ͢Δͷ͔ʁ🤔

  10. ࠓ೥ͷ͸͡Ί͔Βٞ࿦͞Ε͍ͯͨ https://github.com/nodejs/corepack/blob/main/README.md

  11. YarnΛNode.jsʹՃ͑ΔPR͕࡞ΒΕ͕ͨɺ൓ରҙݟ΋͋ͬͨ https://github.com/nodejs/node/pull/37277

  12. TSC಺Ͱ౤ථ͕ߦΘΕͯɺCorepackΛ௥Ճ͢Δํ਑Ͱܾఆ https://github.com/nodejs/node/pull/37277

  13. ԿΛղܾ͢Δͷ͔ʁ🤔

  14. Yarn ͱ pnpm Λผ్Πϯετʔϧ͠ͳͯ͘΋͍͍ • Corepack͕άϩʔόϧʹΠϯετʔϧ͞Ε͍ͯΔ؀ڥͰ͸ɺYarn ͱ pnpmΛผ్Πϯετʔϧ͢Δඞཁ͕ͳ͍ • Yarn΍pnpmΛ࣮ߦͨ͠ͱ͖Corepack͕ͦΕΒͷόΠφϦΛμ΢ϯ

    ϩʔυͯ͘͠ΕΔ • Corepack͸Node.jsʹඪ४όϯυϧ͞ΕΔ༧ఆͳͷͰɺNode.jsΛΠ ϯετʔϧ͢Δ͚ͩͰɺYarn ͱ pnpm΋࢖͑ΔΑ͏ʹͳΔ
  15. Yarn ͱ pnpm Λผ్Πϯετʔϧ͠ͳͯ͘΋͍͍ • CorepackΛάϩʔόϧʹΠϯετʔϧ͞Ε͍ͯΔ؀ڥͰ͸ɺYarn ͱ pnpmΛผ్Πϯετʔϧ͢Δඞཁ͕ͳ͍ • Corepack͸Node.jsʹඪ४όϯυϧ͞ΕΔ༧ఆͳͷͰɺNode.jsΛΠ

    ϯετʔϧ͢Δ͚ͩͰɺYarn ͱ pnpmΛ࢖͑ΔΑ͏ʹͳΔ /PEFKTΛΠϯετʔϧͨ͠ΓόʔδϣϯΞοϓͨ͠Γ͢Δ࣌ɺ :BSO΍QOQNΛผ్Πϯετʔϧ͢Δख͕ؒল͚Δ
  16. • package.jsonͷpackageManagerϑΟʔϧυʹϓϩδΣΫτͰར༻ ͢ΔύοέʔδϚωʔδϟʔͱόʔδϣϯΛهड़͓ͯ͘͠ ➡ύοέʔδϚωʔδϟΛར༻͢Δͱ͖ʹCorepack͕ͦͷύοέʔ δϚωʔδϟʔΛμ΢ϯϩʔυͯ͠Ωϟογϡͯ͘͠ΕΔ ϓϩδΣΫτ͝ͱʹར༻͢ΔύοέʔδϚωʔδϟͱόʔδϣϯΛ੾Γସ͑Δ { "packageManager": "yarn@3.0.1" }

  17. • ϓϩδΣΫτͰར༻͢ΔύοέʔδϚωʔδϟʔͱόʔδϣϯΛ package.jsonʹهड़͓ͯ͘͠ ➡ύοέʔδϚωʔδϟΛར༻͢Δͱ͖ʹCorepack͕ͦͷύοέʔ δϚωʔδϟʔΛμ΢ϯϩʔυͯ͠Ωϟογϡͯ͘͠ΕΔ ϓϩδΣΫτ͝ͱʹύοέʔδϚωʔδϟͱόʔδϣϯΛ੾Γସ͑ { "packageManager": "yarn@3.0.1" }

    ύοέʔδϚωʔδϟʔ΍όʔδϣϯΛνʔϜϝϯόʔؒͰ౷ҰͰ͖ͨΓɺ ΞοϓσʔτͳͲΛ֤ϝϯόʔ͕खಈͰߦΘͳͯ͘΋Α͘ͳΔɻ
  18. ࣮ߦ͢ΔσΟϨΫτϦ͔Β 
 Ұ൪͍ۙpackage.jsonͰ൑ఆ ͨͱ͑͹… packages package-a package-b src package.json src

    package.json package.json “packageManager”: “npm@6.14.14” “packageManager”: “yarn@1.11.10” “packageManager”: “yarn@3.0.1”
  19. Ұ൪͍ۙpackage.jsonͰ൑ఆ ͨͱ͑͹… packages package-a package-b src package.json src package.json package.json

    “packageManager”: “npm@6.14.14” “packageManager”: “yarn@1.11.10” “packageManager”: “yarn@3.0.1” .POPSFQPͰ΋ύοέʔδ͝ͱʹར༻͢ΔύοέʔδϚωʔδϟʔΛ ม͑Δ͜ͱ͕Ͱ͖Δɻ ύοέʔδϚωʔδϟʔͷҠߦ΍όʔδϣϯΞοϓΛ 
 ύοέʔδ୯ҐͰߦ͑Δɻ
  20. ଞͷύοέʔδϚωʔδϟͷར༻Λ੍ݶ͢Δ { "packageManager": "yarn@3.0.1" } $ npm install Usage Error:

    This project is con fi gured to use yarn package.jsonʹఆٛͨ͠ύοέʔδϚωʔδϟҎ֎ͷ ύοέʔδϚωʔδϟΛ࢖͏ͱΤϥʔʹͳΔ
  21. ଞͷύοέʔδϚωʔδϟͷར༻Λ੍ݶ͢Δ { "packageManager": "yarn@3.0.1" } $ npm install Usage Error:

    This project is con fi gured to use yarn Package.jsonʹఆٛͨ͠ύοέʔδϚωʔδϟҎ֎ͷ ύοέʔδϚωʔδϟΛ࢖͏ͱΤϥʔʹͳΔ ؒҧͬͯଞͷύοέʔδϚωʔδϟʔ͕࢖ΘΕΔ͜ͱΛ੍ݶͰ͖Δɻ ΤϥʔϝοηʔδΛݟͯͲͷύοέʔδϚωʔδϟʔΛ 
 ࢖Θͳ͚Ε͹͍͚ͳ͍͔Θ͔Δɻ
  22. • Corepack಺ʹఆٛ͞Ε͍ͯΔσϑΥϧτͷόʔδϣϯ͕࢖ΘΕΔ • 2021/08/30࣌఺Ͱ Yarn͸1.22.11ɺpnpm͸6.11.0 • σϑΥϧτͷόʔδϣϯΛมߋ͢Δʹ͸corepackίϚϯυͰมߋ package.jsonʹࢦఆ͕ແ͍৔߹ $ corepack

    prepare yarn@3.0.1 —activate
  23. • σϑΥϧτͷόʔδϣϯ͕࢖ΘΕΔ • σϑΥϧτͷόʔδϣϯ͸Corepack಺(con fi g.json)Ͱఆٛ͞Ε͍ͯ Δɻ2021/08/30࣌఺Ͱ Yarn͸1.22.11ɺpnpm͸6.11.0 • σϑΥϧτͷόʔδϣϯΛมߋ͢Δʹ͸corepackίϚϯυͰมߋ

    
 `$ corepack parepare yarn@3.0.1 —activate` ϓϩδΣΫτ͝ͱͷࢦఆ͕ແ͍৔߹ $PSFQBDL͕OQNJOTUBMMHZBSO!ʹ૬౰͢ΔίϚϯυΛ༻ҙ͠ ͍ͯΔ
  24. ⚠Corepackͷ஫ҙ఺⚠

  25. ⚠Corepackͷ஫ҙ఺⚠ • npmΛσϑΥϧτͰαϙʔτ͍ͯ͠ͳ͍ • npm install Ͱ Yarn ΍ pnpm

    ΛΠϯετʔϧ͠ͳ͍͜ͱ
  26. npmΛσϑΥϧτͰαϙʔτ͍ͯ͠ͳ͍ { "packageManager": “npm@7.0.0” } $ npm -v 6.14.15 Corepack͸Yarnͱpnpm͚ͩΛαϙʔτɻ

    
 αϙʔτ͍ͯ͠ͳ͍ύοέʔδϚωʔδϟʔʹؔͯ͠͸ɺ ݩʑΠϯετʔϧ͞Ε͍ͯΔόΠφϦ͕࢖ΘΕΔ
  27. $ corepack enable npm $ npm -v 7.0.0 $ yarn

    This project is con fi gured to use npm DPSFQBDLFOBCMFίϚϯυͰOQNͷ TIJNΛ$PSFQBDLͷ؅ཧԼʹ௥Ճ͢Δ $ corepack disable npm $ npm -v 6.14.8 DPSFQBDLEJTBCMFίϚϯυͰOQNͷ TIJNΛ࡟আ͢Δ
  28. طଘͷnpmʹ͸Өڹ͠ͳ͍ https://github.com/nodejs/node/pull/39608 ݩʑΠϯετʔϧ͞Ε͍ͯΔOQNΛ্ॻ͖͠ͳ͍ɻ ͜Ε·Ͱ/PEFKTʹόϯυϧ͞Ε͍ͯΔOQNʹ͸৮Εͳ͍ܗͰ 
 $PSFQBDL͸/PEFKTʹ௥Ճ͞Ε͍ͯΔɻ

  29. npm install -g yarn ͸࣮ߦ͠ͳ͍ • npm 7Ҏ߱ͩͱɺΠϯετʔϧࡁͷύοέʔδΛ࠶ΠϯετʔϧͰ͖ͳ͍ • Yarn

    ͱ pnpm͸Corepack͕͢ͰʹΠϯετʔϧ͍ͯ͠ΔͷͰΤϥʔ $ npm install -g yarn npm ERR! code EEXIST npm ERR! path /path/to/.nvm/versions/node/v14.15.0/bin/yarnpkg npm ERR! EEXIST: fi le already exists • Corepack͕Πϯετʔϧ͢ΔόΠφϦΛ্ॻ͖͠ͳ͍ͨΊʹ΋ɺ 
 npm installΛ࢖ͬͨYarn΍pnpmͷΠϯετʔϧ͸ආ͚ͨ΄͏͕ྑ͍ • Ͳ͏ͯ͠΋࣮ߦ͍ͨ͠৔߹͸—forceΛ෇͚Δ $ npm install -g yarn —force
  30. ͍͔ͭΒ࢖͑Δͷʁ🤔

  31. 8/25 ʹ Corepack௥ՃͷPR͕Node.jsͷ master ʹϚʔδ͞Εͨ https://github.com/nodejs/node/pull/39608 ϚΠόʔόʔδϣϯͰ௥ՃՄೳɻWYͷ ϦϦʔεͰ௥Ճ͞ΕΔ͔΋

  32. Experimentalͳػೳͱͯ͠ϦϦʔε͞ΕΔ༧ఆ https://github.com/nodejs/node/blob/master/doc/api/corepack.md

  33. ࠓ͙͢࢖͍͍ͨͳΒʁ https://www.npmjs.com/package/corepack OQNύοέʔδͱͯ͠΋഑෍͞Ε͍ͯΔͷͰɺ OQNJOTUBMMHDPSFQBDLͰΠϯετʔϧՄೳ

  34. ·ͱΊ • Corepack͕͋Ε͹… • Yarn ΍ pnpm ΛݸผʹΠϯετʔϧ͠ͳͯ͘΋Α͍ • ϓϩδΣΫτ͝ͱʹར༻͢ΔύοέʔδϚωʔδϟͱͦͷόʔδϣϯΛ

    package.jsonʹࢦఆ͢Δ͜ͱ͕Ͱ͖Δ • Corepack͕όϯυϧ͞ΕͨNode.js͸΋͏͙͢ϦϦʔε͞ΕΔ͔΋
  35. https://zenn.dev/teppeis/articles/2021-05-corepack ࢀߟࢿྉ

  36. Thanks. @shisama_ shisama