LoginSignup
1230
1129

More than 3 years have passed since last update.

npmとyarnのコマンド早見表

Last updated at Posted at 2018-07-01

2019/12/24追記

npmとyarnの脆弱性が報告されています。

npmとyarnの脆弱性とpostinstall - Cybozu Inside Out | サイボウズエンジニアのブログ
npmの利用者としてやるべきことは、

  • npmのバージョンを6.13.4以上にあげる
  • yarnのバージョンを1.21.1以上にあげる

概要

npmとyarnの「よく使う」から「ときどき使う」くらいまでのコマンド早見表です。

環境

  • Windows 10 Professional
  • Nodist 0.8.8

参考

バージョン

Node.jsおよびnpmはNodistでインストール、yarnはnpmでインストールしました。

Node.js

> node --version
v8.11.2

npm

> npm --version
6.1.0

yarn

> yarn --version
1.7.0

コマンド

実行内容 npm yarn
インストール
パッケージ一覧
Global
npm ls -g --depth=0
Global
yarn global list --depth=0
Local
npm ls --depth=0
Local
yarn list --depth=0
binの場所 Global
npm bin -g
Global
yarn global bin
Local
npm bin
Local
yarn bin
node_modulesの場所 Global
npm root -g
Global
N/A
Local
npm root
Local
N/A
インストール Global
npm install -g {package}
Global
yarn global add {package}
Local

:asterisk: package.jsonに記録されているパッケージをインストール

npm install

:asterisk: dependenciesにインストール

npm install --save {package}

:asterisk: devDependenciesにインストール

npm install --save-dev {package}
Local

:asterisk: package.jsonに記録されているパッケージをインストール

yarn install

:asterisk: dependenciesにインストール

yarn add {package}

:asterisk: devDependenciesにインストール

yarn add --dev {package}
アンインストール Global
npm uninstall -g {package}
Global
yarn global remove {package}
Local

:asterisk: dependenciesからアンインストール

npm uninstall --save {package}

:asterisk: devDependenciesからアンインストール

npm uninstall --save-dev {package}
Local

:asterisk: dependenciesからアンインストール

yarn remove {package}

:asterisk: devDependenciesからアンインストール

yarn remove {package}
アップデート Global

:asterisk: 全体をアップデート

npm update -g

:asterisk: 指定したパッケージをアップデート

npm update -g {package}
Global

:asterisk: 全体をアップデート

yarn global upgrade

:asterisk: 指定したパッケージをアップデート

yarn global upgrade {package}
Local

:asterisk: 全体をアップデート

npm update

:asterisk: 指定したパッケージをアップデート

npm update {package}
Local

:asterisk: 全体をアップデート

yarn upgrade

:asterisk: 指定したパッケージをアップデート

yarn upgrade {package}
パッケージの情報 Global
npm info -g {package}
Global
yarn info {package}
Local
npm info {package}

:asterisk: jsonで出力

npm info {package} --json

:asterisk: 特定のフィールドの情報 (e.g. description)

npm info {package} description
Local
yarn info {package}

:asterisk: jsonで出力

yarn info {package} --json

:asterisk: 特定のフィールドの情報 (e.g. description)

yarn info {package} description
古いパッケージ Global

:asterisk: 全体

npm outdated -g

:asterisk: 指定したパッケージ

npm outdated -g {package}
Global

:asterisk: 全体

N/A

:asterisk: 指定したパッケージ

N/A
Local

:asterisk: 全体

npm outdated

:asterisk: 指定したパッケージ

npm outdated {package}
Local

:asterisk: 全体

yarn outdated

:asterisk: 指定したパッケージ

yarn outdated {package}
キャッシュ

:asterisk: クリア

npm cache verify

:asterisk: 強制クリア

npm cache clean --force

:asterisk: 一覧

N/A

:asterisk: ディレクトリ

N/A

:asterisk: クリア

yarn cache clean

:asterisk: 強制クリア

N/A

:asterisk: 一覧

yarn cache list

:asterisk: ディレクトリ

yarn cache dir
コンフィグ

:asterisk: 一覧

npm config list

:asterisk: デフォルト

npm config list -l

:asterisk: プロパティ名指定

npm config get {プロパティ名}

:asterisk: json

npm config list --json

:asterisk: 一覧

yarn config list

:asterisk: デフォルト

N/A

:asterisk: プロパティ名指定

yarn config get {プロパティ名}

:asterisk: json

N/A
スクリプト

:asterisk: 一覧

npm run

:asterisk: 実行

npm run {script}

:asterisk: 環境変数の表示

npm run env

:asterisk: 一覧

yarn run

:asterisk: 実行

yarn run {script}

:asterisk: 環境変数の表示

yarn run env
ローカルパッケージの実行 npx {package}

npxの例を参照

yarn -s run {package}

 

npm キャッシュのクリア

npm バージョン5以上でnpm cache clearを実行するとエラーになります。これは--forceオプションが必須となっているためです。

npm ERR! As of npm@5, the npm cache self-heals from corruption issues and data extracted from the cache is guaranteed to be valid. If you want to make sure everything is consistent
, use 'npm cache verify' instead. On the other hand, if you're debugging an issue with the installer, you can use `npm install --cache /tmp/empty-cache` to use a temporary cache in
stead of nuking the actual one.
npm ERR!
npm ERR! If you're sure you want to delete the entire cache, rerun this command with --force.

通常は、キャッシュが破損していないか一貫性を確認するためには、代わりにcache verifyとします。

> npm cache verify

キャッシュ全体を削除するには従来どおり--forceを指定します。

> npm cache clear --force

通常のキャッシュを利用せずに、一時的なキャッシュを利用するには--cacheとパスを指定します。

> npm install --cache /tmp/empty-cache

npxの例

npxを使うとローカル(node_modules\.bin)にインストールされたパッケージをパスを指定せずに実行することができます。

インストール

> npm install -g npx
> npx --version
10.2.0

npxの使用例

node_modules\.binにインストールされているuuidを実行してみます。

> npm info uuid description
RFC4122 (v1, v4, and v5) UUIDs
> npx uuid v1
0e98f3f0-7df3-11e8-bac0-c5a39d5528ee

yarnの場合

yarnの場合はrunコマンドで実行できます。

> yarn -s run uuid v1
90303fd0-7df9-11e8-8f9f-4341d8d5b67a

npm auditの例

auditはnpm 6.0で追加された脆弱性に関するレポートとfixを行ってくれるコマンドです。
パッケージのインストール時に、脆弱性(vulnerabilities)が報告されているパッケージがあった場合、下記のように出力されます。

> npm install

// ...省略...

audited 10504 packages in 15.43s
found 4 vulnerabilities (1 low, 1 moderate, 2 critical)
  run `npm audit fix` to fix them, or `npm audit` for details

npm audit

セキュリティレポートを出力します。解決方法がある場合はその方法も出力されます。

> npm audit

                       === npm audit security report ===

# Run  npm install --save-dev url-loader@1.0.1  to resolve 1 vulnerability
SEMVER WARNING: Recommended action is a potentially breaking change

  Moderate        Regular Expression Denial of Service

  Package         mime

  Dependency of   url-loader [dev]

  Path            url-loader > mime

  More info       https://nodesecurity.io/advisories/535



# Run  npm update macaddress --depth 5  to resolve 2 vulnerabilities

  Critical        Command Injection

  Package         macaddress

  Dependency of   css-loader [dev]

  Path            css-loader > cssnano > postcss-filter-plugins > uniqid >
                  macaddress

  More info       https://nodesecurity.io/advisories/654




  Critical        Command Injection

  Package         macaddress

  Dependency of   optimize-css-assets-webpack-plugin [dev]

  Path            optimize-css-assets-webpack-plugin > cssnano >
                  postcss-filter-plugins > uniqid > macaddress

  More info       https://nodesecurity.io/advisories/654



# Run  npm update fill-range --depth 6  to resolve 1 vulnerability

  Low             Cryptographically Weak PRNG

  Package         randomatic

  Dependency of   webpack-dev-server [dev]

  Path            webpack-dev-server > http-proxy-middleware > micromatch >
                  braces > expand-range > fill-range > randomatic

  More info       https://nodesecurity.io/advisories/157



found 4 vulnerabilities (1 low, 1 moderate, 2 critical) in 10534 scanned packages
  run `npm audit fix` to fix 3 of them.
  1 vulnerability requires semver-major dependency updates.

npm audit fix

レポートされている脆弱性を解決します。
下記の例では4件中3件が解決し、1件が未解決になっています。(脆弱性があるパッケージをアップデートすると破壊的変更があるためfixしなかったという状態です。)

> npm audit fix

added 4 packages from 16 contributors, removed 2 packages and updated 2 packages in 15.415s
fixed 3 of 4 vulnerabilities in 10534 scanned packages
  1 package update for 1 vuln involved breaking changes
  (use `npm audit fix --force` to install breaking changes; or do it by hand)

npm audit fix --force

強制的にfixするには--forceフラグを付けます。

> npm audit fix --force

+ url-loader@1.0.1
added 73 packages from 29 contributors and updated 2 packages in 16.369s
fixed 1 of 1 vulnerability in 10532 scanned packages
  1 package update for 1 vuln involved breaking changes
  (installed due to `--force` option)

npm audit

すべて解決したかどうかをもう一度レポートで確認します。

> npm audit

                       === npm audit security report ===

found 0 vulnerabilities
 in 10540 scanned packages

Usage

npm

コマンドの使い方一覧を表示

> npm -l

各コマンドの使い方

> npm <command> -h

yarn

各コマンドの使い方

> yarn help <command>
1230
1129
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1230
1129