
目次
npm installでvulnerabilitiesが表示されたら
プロジェクトフォルダをGitHubなどからgit cloneして、npm installすると以下のようにvulnerabilities〜が表示されることがあります。

これが表示されたらすぐに直すべきなのですが、「vulnerabilities」が何なのか知らず、修正せずに放置する人をたまに見かけます。
vulnerabilitiesの意味と読み方
vulnerabilitiesとは脆弱性という意味です。
読み方は「バルネラビリティーズ」で、単数形はvulnerability(バルネラビリティ)です。
しかし、日本では「脆弱性」と言うことが一般的なので、「このプロジェクトにバルネラビリティがあって〜」などと言っても大半の人には伝わらないでしょう。
npm installでvulnerabilitiesが出たときの修正方法
まず、vulnerabilitiesの表示が出たら以下の部分に注目してください。
13 vulnerabilities (1 low, 6 moderate, 6 high)
これはセキュリティ脆弱性が合計13件見つかったことを示しています。
- low(低):1件
- moderate(中):6件
- high(高):6件
その次に、以下のような内容が表示されます。
To address all issues, run:
npm audit fix
Run `npm audit` for details.
「npm audit」は脆弱性のあるパッケージの詳細を見るためのコマンドです。
試しに実行すると以下のような内容が表示されます。
$ npm audit
# npm audit report
@babel/helpers <7.26.10
Severity: moderate
Babel has inefficient RexExp complexity in generated code with .replace when transpiling named capturing groups - https://github.com/advisories/GHSA-968p-4wvh-cqc8
fix available via `npm audit fix`
node_modules/@babel/helpers
astro <=5.3.1
Severity: high
DOM Clobbering Gadget found in astro's client-side router that leads to XSS - https://github.com/advisories/GHSA-m85w-3h95-hcf9
Atro CSRF Middleware Bypass (security.checkOrigin) - https://github.com/advisories/GHSA-c4pw-33h3-35xw
Astro's server source code is exposed to the public if sourcemaps are enabled - https://github.com/advisories/GHSA-49w6-73cw-chjr
Depends on vulnerable versions of cookie
Depends on vulnerable versions of esbuild
fix available via `npm audit fix`
node_modules/astro
# 長いので以下略
脆弱性のあるパッケージ名やSeverity (重大度)などが一覧で表示されます。
「fix available via `npm audit fix`」と記載されているものは「npm audit fix」を実行すれば自動で修正可能です。
$ npm audit fix
added 24 packages, removed 81 packages, changed 154 packages, and audited 400 packages in 23s
158 packages are looking for funding
run `npm fund` for details
# npm audit report
esbuild <=0.24.2
Severity: moderate
esbuild enables any website to send any requests to the development server and read the response - https://github.com/advisories/GHSA-67mh-4wv8-2f99
fix available via `npm audit fix --force`
Will install astro@5.7.4, which is a breaking change
node_modules/esbuild
astro <=0.0.12 || 0.10.0 - 5.3.1
Depends on vulnerable versions of esbuild
Depends on vulnerable versions of vite
node_modules/astro
vite 0.11.0 - 6.1.5
Depends on vulnerable versions of esbuild
node_modules/vite
3 moderate severity vulnerabilities
To address all issues (including breaking changes), run:
npm audit fix --force
前述の例の場合、「npm audit fix」を実行したら「high」の脆弱性がなくなりました。
多くの場合は「npm audit fix」を実行するだけで修正できます。
breaking changes (破壊的変更) を含む場合は「npm audit fix --force」でないとすべて修正できないことがあります。
続けて「npm audit fix --force」を実行してみます。
$ npm audit fix --force
npm warn using --force Recommended protections disabled.
npm warn audit Updating astro to 5.7.4, which is a SemVer major change.
added 51 packages, removed 78 packages, changed 23 packages, and audited 373 packages in 58s
150 packages are looking for funding
run `npm fund` for details
found 0 vulnerabilities
0 vulnerabilities (脆弱性0) になりました。
npm outdatedコマンドで最新バージョンを確認
npm audit fixは脆弱性のあるパッケージのバージョンを自動アップデートするもので、すべてのパッケージを最新バージョンにするわけではありません。
現在使用されているパッケージに最新バージョンがあるか確認したい場合は、npm outdatedコマンドで確認できます。
$ npm outdated
Package Current Wanted Latest Location Depended by
@astrojs/check 0.5.10 0.5.10 0.9.4 node_modules/@astrojs/check my-astro
typescript 5.4.5 5.8.3 5.8.3 node_modules/typescript my-astro
このときに「Current」が「Wanted」よりもバージョンが低い場合は、npm updateコマンドでバージョンをWantedにアップデートできます。
ちなみに「Wanted」は推奨バージョンではなくpackage.jsonに書かれているバージョン指定の範囲内でインストール可能な最新バージョンです。
この例の場合だとpackage.jsonに「"typescript": "^5.4.5"」のように書かれています。
メジャーバージョンは「^」により5で固定されていますが、マイナーバージョンとパッチバージョンはnpm updateでアップデート可能です。
"typescript": "^[メジャーバージョン].[マイナーバージョン].[パッチバージョン]"
試しにnpm updateを実行するとtypescriptがアップデートされたため、npm outdatedを実行しても表示されなくなります。
$ npm update
added 27 packages, removed 17 packages, changed 33 packages, and audited 381 packages in 58s
151 packages are looking for funding
run `npm fund` for details
found 0 vulnerabilities
$ npm outdated
Package Current Wanted Latest Location Depended by
@astrojs/check 0.5.10 0.5.10 0.9.4 node_modules/@astrojs/check my-astro
ただし、npm updateコマンドではpackage.json内のdependenciesのバージョンはアップデートされないです。
dependenciesのバージョンも変更したい場合は手動で書き換えるか、npm-check-updatesを使用して更新する必要があります。
$ npm install -g npm-check-updates
added 1 package in 841ms
$ ncu
Checking package.json
[====================] 3/3 100%
@astrojs/check ^0.5.10 → ^0.9.4
typescript ^5.4.5 → ^5.8.3
Run ncu -u to upgrade package.json
# ncu -u コマンドでpackage.jsonを上記のバージョンに変更できる
ncu -u コマンドはpackage.jsonのバージョンを書き換えるだけなので、書き換えたバージョンに更新するには別途、npm installコマンドの実行が必要です。
まとめ
- npm install 実行後に表示される「vulnerabilities」は脆弱性という意味
- npm audit を実行すれば脆弱性のあるパッケージ一覧が確認できる
- npm audit fix で脆弱性のあるパッケージを自動修正できる
- npm audit fix --force でbreaking changes (破壊的変更) を含む脆弱性のあるパッケージを自動修正できる
- npm outdatedコマンドでパッケージの最新バージョンを確認できる
- npm updateコマンドでパッケージのバージョンをアップデートできる
脆弱性を残したままリリースすると、バグやセキュリティ上の問題が発生する可能性があります。
また、脆弱性を残したままクライアントに制作物を納品すると、トラブルの原因になるケースもありますので注意してください。