npm

維基百科,自由的百科全書
跳至導覽 跳至搜尋

npm
File:Npm-logo.svg
開發者艾薩克·施呂特、福里斯特·諾維爾、羅伯特·科瓦爾斯基、多梅尼克·德尼科拉、蒂姆·奧克斯利、埃文·馬爾等
目前版本
    Module:EditAtWikidata第29行Lua錯誤:attempt to index field 'wikibase' (a nil value)
    原始碼庫
    • {{URL|example.com|可选的显示文本}}
    Module:EditAtWikidata第29行Lua錯誤:attempt to index field 'wikibase' (a nil value)
    程式語言JavaScript
    引擎
      Module:EditAtWikidata第29行Lua錯誤:attempt to index field 'wikibase' (a nil value)
      平台Linux
      macOS
      Windows
      授權條款
        Module:EditAtWikidata第29行Lua錯誤:attempt to index field 'wikibase' (a nil value)

        npm(全稱 Node Package Manager,即「node套件管理器」)是Node.js預設的、用JavaScript編寫的軟體套件管理系統

        歷史[編輯]

        npm完全用JavaScript寫成,最初由艾薩克·施呂特(Isaac Z. Schlueter)開發。艾薩克表示自己意識到「模組管理很糟糕」的問題,並看到了PHPPEARPerlCPAN等軟體的缺點,於是編寫了npm。[1]

        2020年3月16日,GitHub CEO Nat Friedman 宣布 GitHub 已簽署收購 NPM(npm 背後的公司)的協定,並表示 npm 加入 GitHub 後會繼續免費提供公共軟體註冊中心服務。

        說明[編輯]

        npm會隨著Node.js自動安裝[2]。npm模組儲存庫提供了一個名為「registry」的查詢服務,使用者可通過本機的npm命令下載並安裝指定模組。此外使用者也可以通過npm把自己設計的模組分發到registry上面[3]

        registry上面的模組通常採用CommonJS格式,而且都包含一個JSON格式的中繼檔案[4]。截至2016年7月,npm的registry上面已經註冊了超過280,000個模組[5]

        npm的模組以「先搶先贏」的原則註冊,各模組作者不會發生混亂。然而一旦有人撤回自己釋出的模組,那麼不僅會使依附那個模組的專案出現問題,還會帶來安全風險[6]。例如有一個模組叫做「left-pad」,其中只有一個字串對齊的功能。但是,當作者把它從registry裡面移除之後,許多模組便無法正確組建[7]

        npm的registry沒有稽核機制,因此會存在一些低品質、不安全甚至有害的模組[4],然而npm伺服器的管理員也可以刪除有害模組並阻止不懷好意的使用者[8]

        另外也有人為npm製作了統計功能,這樣可以讓開發者了解各模組的使用情況,幫助他們選擇合適的模組。[9]

        使用[編輯]

        npm可以管理本機專案的所需模組並自動維護相依性,也可以管理全域安裝的JavaScript工具[10]

        如果一個專案中存在package.json檔案,那麼使用者可以直接使用npm install命令自動安裝和維護當前專案所需的所有模組[11]。在package.json檔案中,開發者可以指定每個相依項的版本範圍,這樣既可以保證模組自動更新,又不會因為所需模組功能大幅變化導致專案出現問題[12]。開發者也可以選擇將模組固定在某個版本之上[13]

        在中國大陸,由於防火長城的干擾,開發者可能需要更換軟體源才能正常下載和安裝模組。

        意外[編輯]

        2016年3月,Azer Koçulu移除了他受歡迎的 left-pad 套件。儘管 left-pad 在三小時後重新發布,但它造成了廣泛的破壞。[14]npm之後修改了有關政策,以防止將來發生類似事件。[15]

        2022年3月,布蘭登·野崎·米勒發布了包含惡意代碼的 node-ipc 套件版本。使用 node-ipcVue.js 沒有將其套件固定到安全版本,這意味著 Vue.js 的部分使用者可能會受到惡意代碼的影響。[16]

        參考文獻[編輯]

        1. ^ Schlueter, Isaac Z. Forget CommonJS. It's dead. **We are server side JavaScript.**. GitHub. 25 March 2013 [2015-01-05]. (原始內容存檔於2015-05-08). 
        2. ^ Dierx, Peter. A Beginner's Guide to npm — the Node Package Manager. sitepoint. 30 March 2016 [22 July 2016]. (原始內容存檔於2017-02-04). 
        3. ^ Ampersand.js. Ampersand.js - Learn. ampersandjs.com. [22 July 2016]. (原始內容存檔於2016-10-04). 
        4. ^ 4.0 4.1 Ojamaa, Andres; Duuna, Karl. Assessing the Security of Node.js Platform. IEEE Xplore. 2012 [22 July 2016]. (原始內容存檔於2019-10-18). 
        5. ^ Kennedy, Hugh; DeVay, Paul. Understanding npm. Nsight. [22 July 2016]. (原始內容存檔於2016年7月8日). 
        6. ^ Yegulalp, Serdar. How one yanked JavaScript package wreaked havoc. InfoWorld. 23 March 2016 [22 July 2016]. (原始內容存檔於2016-12-05). 
        7. ^ Williams, Chris. How one developer just broke Node, Babel and thousands of projects in 11 lines of JavaScript. The Register. [17 April 2016]. (原始內容存檔於2016-03-24). 
        8. ^ npm, Inc. npm. npm. [22 October 2016]. (原始內容存檔於2016-10-22). 
        9. ^ npm-stat: download statistics for NPM packages. [2016-12-07]. (原始內容存檔於2016-08-11). 
        10. ^ Ellingwood, Justin. How To Use npm to Manage Node.js Packages on a Linux Server. DigitalOcean. [22 October 2016]. (原始內容存檔於2016-10-22). 
        11. ^ npm-install. docs.npmjs. [22 October 2016]. (原始內容存檔於2016-12-03). 
        12. ^ semver. docs.npmjs. [22 October 2016]. (原始內容存檔於2016-12-03). 
        13. ^ npm-version. docs.npm. [29 October 2016]. (原始內容存檔於2016-12-03). 
        14. ^ Williams, Chris; Chief, Editor in. How one developer just broke Node, Babel and thousands of projects in 11 lines of JavaScript. www.theregister.com. [2023-09-10]. (原始內容存檔於2023-10-16) (English). 
        15. ^ npm Blog Archive: changes to npm’s unpublish policy. blog.npmjs.org. [2023-09-10]. (原始內容存檔於2023-06-23). 
        16. ^ BIG sabotage: Famous npm package deletes files to protest Ukraine war. BleepingComputer. [2023-09-10]. (原始內容存檔於2022-03-17) (en-us). 

        外部連結[編輯]

        參見[編輯]