Node.jsのパッケージ管理npmを理解する

Node.jsのパッケージ管理npmを理解する

2020-05-16


npm - Node.jsのパッケージ管理

npmには、オンラインリポジトリと、パッケージ管理のコマンドラインツールの2つを指す。

今回、コマンドラインツールとしてのnpmについて説明します。

Node.jsのパッケージ管理 npm

Node.jsの標準のパッケージ管理は、npmコマンドで行います。

npmでは、npm installコマンドでインストールした場合、プロジェクトのルートディレクトリ
のpackage.jsonというファイルにインストールしたプラグインが追記されます。

コマンドラインnpm実行時の警告・エラー

1. npm install - “npm WARN optional SKIPPING OPTIONAL DEPENDENCY: fsevents…”

状況

1
npm WARN notsup SKIPPING OPTIONAL DEPENDENCY: Unsupported platform for fsevents@1.2.13: wanted {"os":"darwin","arch":"any"} (current: {"os":"linux","arch":"x64"})

How to solve npm install throwing fsevents warning on non-Mac OS?

原因

MacOS限定のモジュールがnpm installによって読み込まれた。

対策

OPTIONAL DEPENDENCYと表示されて必要がないオプションモジュールなので、npm installから除外します。

対応方法

FoggyDay Oct 14’19 at 16:17のコメントを参照して、次のコマンドを実行します。

1
npm install -f

2. npm install - npm fundの表示の意味は?

パッケージの開発者への寄付を募るメッセージであり、npm fundコマンドを実行すると、どのパッケージが寄付対象であるか示してくれる。

3. npm install - “found 1 low severity vulnerability”

npm installコマンド実行時に、脆弱性に関する警告が発生する。npm auditコマンドで、脆弱性のあるパッケージをリストアップする。

状況

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
                                                                          
=== npm audit security report ===

┌──────────────────────────────────────────────────────────────────────────────┐
│ Manual Review │
│ Some vulnerabilities require your attention to resolve │
│ │
│ Visit https://go.npm.me/audit-guide for additional guidance │
└──────────────────────────────────────────────────────────────────────────────┘
┌───────────────┬──────────────────────────────────────────────────────────────┐
│ Low │ Prototype Pollution │
├───────────────┼──────────────────────────────────────────────────────────────┤
│ Package │ minimist │
├───────────────┼──────────────────────────────────────────────────────────────┤
│ Patched in │ >=0.2.1 <1.0.0 || >=1.2.3 │
├───────────────┼──────────────────────────────────────────────────────────────┤
│ Dependency of │ hexo │
├───────────────┼──────────────────────────────────────────────────────────────┤
│ Path │ hexo > swig-templates > optimist > minimist │
├───────────────┼──────────────────────────────────────────────────────────────┤
│ More info │ https://npmjs.com/advisories/1179 │
└───────────────┴──────────────────────────────────────────────────────────────┘
found 1 low severity vulnerability in 253 scanned packages
1 vulnerability requires manual review. See the full report for details.

原因

呼び出し元のoptimistが、minimistの古いバージョンに依存しているため

対策

npm auditを実行して解決方法が存在するか確認する。

1.脆弱性に関する解決方法が公開されている場合、解決のためのコマンドが表示されるため、コマンドを実行して解決する(例)。

1
2
=== npm audit security report ===
# Run npm update lodash --depth 2 to resolve 1 vulnerability

2.解決方法がない場合、npm audit fixを実行してセキュリティパッチを当てるか、それでも解決しない低度の脆弱性であれば、そのままにしておく。

そもそも未知の脆弱性が存在する場合もあり、npm auditで解決できるとは限らない。

1
2
3
4
npm install -g yarn
npm install yarn
yarn install
yarn upgrade