目前工作上遇到一個需求,公司使用 Verdaccio 建立一個 Node.js private proxy registry,裡面有一些建置環境需要的package,Verdaccio建在私有網域,但是公司的建置機器卻是在雲端服務,目前無法連回公司網域,所以一開始的思路是找可以離線安裝的方法。
https://docs.npmjs.com/cli/v6/configuring-npm/package-json#bundleddependencies
This defines an array of package names that will be bundled when publishing the package.
In cases where you need to preserve npm packages locally or have them available through a single file download, you can bundle the packages in a tarball file by specifying the package names in thebundledDependenciesarray and executingnpm pack.
可以在package.json中設置pack時需要一起打包的dependencies package,這樣使用npm pack時就會一起包進去。
不過因為這樣的需求就要改package.json且造成檔案變大好像不太好,這裡有一個別人提供的腳本可以自動化。
https://www.npmjs.com/package/shrinkpack
這個工具的運作模式可以看這個章節 ,基本上使用 npm ci 是基於package-lock.json的package安裝方法,該工具將所有依賴的package抓到node_shrinkpack資料夾並改寫package-lock.json,所以只要使用 npm ci --offline就可以離線安裝所有依賴package。
這個方法需要npm version是7以上才可以使用。