npm install

パッケージと実行環境

書き方

npm install [<package>] [-D|-g]

package.jsonに書かれた依存をnode_modulesへ入れ、package-lock.jsonを更新します。npm 7からpeer依存も一緒に入り、--productionは--omit=devに置き換わりました。

よく使うオプション

オプション意味
-Dsave as a devDependency
-ginstall globally, onto PATH
-Esave the exact version, without a caret
--omit=devskip devDependencies; replaces the old --production
--legacy-peer-depsignore peer conflicts the way npm 6 did
--ignore-scriptsdo not run install scripts
--forceoverwrite cache and conflict checks

npm install

installs what package.json asks for and updates package-lock.json

npm install -D vitest

adds a dev-only dependency

npm install react@18.3.1

installs one exact version and records it

何をどこに入れるかが問題です。このプロジェクトの中か、マシン全体かを先に決めてください。

読み方

  • 角括弧 [ ] は省いてよい部分です。
  • 三点 … は複数並べられるという意味です。
  • オプションは大文字小文字を区別します — -r と -R が別物のコマンドもあります。

よくある質問

Q. npm install は何をしますか。

package.jsonに書かれた依存をnode_modulesへ入れ、package-lock.jsonを更新します。npm 7からpeer依存も一緒に入り、--productionは--omit=devに置き換わりました。

Q. どう打ちますか。

npm install [<package>] [-D|-g] — 角括弧は省いてよい部分です。

Q. よく使うオプションはいくつですか。

ここにまとめたのは7個です。全部は man npm にあります。このコマンドはパッケージと実行環境の仲間です。

関連するコマンド

man ページ: man npm-install