🦁

npmでインストールしたOSSのライセンスを調べたい

2024/03/06に公開

こんにちわ、かねこです。

今回は最近業務でnpmでインストールしたOSSのライセンスを調べる機会があったので、そのやり方を自分自身のメモがてら記事に残そうと思います〜

license-checkerをインストール

OSSのライセンスを調べられるパッケージである、license-checkerをグローバルにインストールする。
npm install -g license-checker
https://www.npmjs.com/package/license-checker

ツールなので、グローバルインストールで問題ないが他のメンバーなどに共有したい等であれば、プロジェクト単位でインストールするのもいいと思います。
ただ、license-checkerも自身の依存ツリーも結果に出てきてしまうので、注意。

実践

プロジェクト内でlicense-checkerを実行する。

├─ @aashutoshrathi/word-wrap
│  ├─ licenses: MIT
│  ├─ repository: https://github.com/aashutoshrathi/word-wrap
│  ├─ publisher: Jon Schlinkert
│  ├─ url: https://github.com/jonschlinkert
│  ├─ path: /Users/kanekoryouma/developments/homepage-create-template/node_modules/@aashutoshrathi/word-wrap
│  └─ licenseFile: /Users/kanekoryouma/developments/homepage-create-template/node_modules/@aashutoshrathi/word-wrap/LICENSE
├─ @babel/code-frame
│  ├─ licenses: MIT
│  ├─ repository: https://github.com/babel/babel
│  ├─ publisher: The Babel Team
│  ├─ url: https://babel.dev/team
│  ├─ path: /Users/kanekoryouma/developments/homepage-create-template/node_modules/@babel/code-frame
│  └─ licenseFile: /Users/kanekoryouma/developments/homepage-create-template/node_modules/@babel/code-frame/LICENSE
├─ @babel/helper-validator-identifier
│  ├─ licenses: MIT
│  ├─ repository: https://github.com/babel/babel
│  ├─ publisher: The Babel Team
│  ├─ url: https://babel.dev/team
│  ├─ path: /Users/kanekoryouma/developments/homepage-create-template/node_modules/@babel/helper-validator-identifier
│  └─ licenseFile: /Users/kanekoryouma/developments/homepage-create-template/node_modules/@babel/helper-validator-identifier/LICENSE
├─ @babel/highlight
│  ├─ licenses: MIT
│  ├─ repository: https://github.com/babel/babel
│  ├─ publisher: The Babel Team
│  ├─ url: https://babel.dev/team
│  ├─ path: /Users/kanekoryouma/developments/homepage-create-template/node_modules/@babel/highlight
│  └─ licenseFile: /Users/kanekoryouma/developments/homepage-create-template/node_modules/@babel/highlight/LICENSE
├─ @babel/runtime
│  ├─ licenses: MIT
│  ├─ repository: https://github.com/babel/babel
│  ├─ publisher: The Babel Team
│  ├─ url: https://babel.dev/team
│  ├─ path: /Users/kanekoryouma/developments/homepage-create-template/node_modules/@babel/runtime
│  └─ licenseFile: /Users/kanekoryouma/developments/homepage-create-template/node_modules/@babel/runtime/LICENSE
├─ @emotion/cache
│  ├─ licenses: MIT
│  ├─ repository: https://github.com/emotion-js/emotion/tree/main/packages/cache
│  ├─ path: /Users/kanekoryouma/developments/homepage-create-template/node_modules/@emotion/cache
│  └─ licenseFile: /Users/kanekoryouma/developments/homepage-create-template/node_modules/@emotion/cache/LICENSE

なんかいっぱい出てきた。
プロジェクトの直接の依存パッケージであるlicense-checkerだけでなく、その依存ツリーのパッケージ全てが出力されます。
devDependenciesのパッケージは別に出力されなくてもいいので、--productionオプションを指定するとdependenciesだけが出力されます。
また、直接の依存パッケージのみを出力する--directというオプションもあるのですが、最新バージョンのv25.0.1だとうまく機能しませんでした、、、(残念)
https://github.com/davglass/license-checker/issues/191

便利そうなオプション

license-checkerには上で紹介したオプション以外にも色々オプションがあるので、githubを見てみたり、
license-checker --helpでオプションの一覧が確認できます。
個人的によく使いそうだなと思ったオプションだけ一部紹介いたします。

オプション 内容
--summary ライセンス毎のサマリーを表示させる。とりあえず、ライセンスの種類を把握するのに良さそう
--production dependenciesのみを表示させる。ライセンス一覧を表示させるページが必要な場合はこのオプションを使えば良さそう
--json JSON形式で出力してくれる
--csv CSV形式で出力してくれる

最後に

今回は、npmでインストールしたOSSのライセンスを調べる方法について紹介させていただきました。
今までOSSのライセンスを調べることがなかったので、いい機会でした✨

今後もこういった自分の困りポイントだったり設定手順だったりを発信していけたらなと思います🐈

最後に、toraco株式会社ではエンジニアを積極採用中です!
フロントエンドエンジニア、バックエンドエンジニア、クラウドインフラエンジニアなど職種問わず、様々な技術領域にチャレンジできます。また、PM(プロジェクトマネージャー) や EM(エンジニアリングマネージャー)のキャリアパスも用意しています。
興味のある方は Wantedly,typeの募集をぜひ読んでください。
https://www.wantedly.com/companies/company_5649245
https://type.jp/job-1/1324043_detail/?pathway=5

toraco株式会社のテックブログ

Discussion