yarnを使おう
npmはNode.js標準のパッケージマネージャー
皆さんはNode.js使ってますか?
フロントエンドエンジニアの方はもちろん、最近ではサーバーサイドの方もNode.jsを使うケースが増えてきていると思います。
そして、Node.jsを取り扱う上で避けて通れないのはパッケージ(モジュール)管理ですよね。
Node.jsはnpmというパッケージマネージャーを標準で搭載しています。
Build amazing things Essential JavaScript development tools that help you go to market faster and build powerful applications using modern open source code.npm | build amazing things
yarnの登場
そして、長らくnpm一強でしたがyarnというパッケージマネージャーが登場し、脚光を浴び始めています。
高速で、信頼性が高く、そして安全な依存関係の管理 Yarn はダウンロードしたパッケージをキャッシュするので、再ダウンロードは不要です。さらにリソースを最大限利用できるように処理を並列化するので、インストール時間はかつてないほど高速です。nYarn
手順
Homebrewでインストール
yarnはHomebrewでインストールする事が出来ます。
==> Installing yarn
==> Downloading https://yarnpkg.com/downloads/1.17.3/yarn-v1.17.3.tar.gz
==> Downloading from https://github-production-release-asset-2e65be.s3.amazonaws.com/49970642/114f8b80-a477-11e9-9a08-70f1f8027b9e?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=AKIAIWNJYAX4CSVEH53A%2F20190902%2Fus
######################################################################## 100.0%
🍺 /usr/local/Cellar/yarn/1.17.3: 14 files, 5MB, built in 9 seconds
簡単にインストール完了です。
確認
以下のコマンドで確認可能です。
1.17.3
init
まずは初期化をしましょう。
以下のコマンドで対話式で設定を求められます。
yarn init v1.17.3
question name (SampleApp):
question version (1.0.0):
question description: Sample App.
question entry point (index.js):
question repository url (git@github.com:XXX/YYY.git):
question author: Blogenist
question license (MIT):
question private:
success Saved package.json
✨ Done in 41.74s.
すると以下のようなpackage.jsonが生成されると思います。
{
"name": "SampleApp",
"version": "1.0.0",
"description": "Sample App.",
"main": "index.js",
"repository": "git@github.com:XXX/YYY.git",
"author": "Blogenist",
"license": "MIT"
}
基本的なコマンドの比較
yarnの基本的なコマンドは以下の通りです。
| コマンド | 用途 |
|---|---|
yarnまたはyarn install | package.jsonを元にモジュールをインストール |
yarn install --flat | 複数のモジュールでバージョンの異なるモジュールを読み込んでいた場合に |
yarn install --force | 強制的に再インストール |
yarn install --production | devDependenciesに記載しているパッケージはインストールしない。 また、 NODE_ENVにproductionが設定されていればこのオプションをつけなくても、自動的にproduction用インストールがされます。 |
yarn global add {モジュール名} | グローバルにパッケージ追加 |
yarn add {モジュール名} | パッケージ追加 |
yarn add --dev|-D {モジュール名} | devDependenciesにパッケージ追加 |
yarn remove {package} | パッケージ削除 |
他にも、yarn upgradeやyarn config、yarn cacheなどの便利なコマンドがありますが、基本的には上記のコマンドを使っていく事になると思います。
終わりに
以上のように、npmがより使いやすく高速になったものがyarnと覚えておくと良いでしょう。
今後のNode.js開発ではnpmではなくyarnを使うようにしてみてください♪

Build amazing things
Essential JavaScript development tools that help you go to market faster and build powerful applications using modern open source code.
高速で、信頼性が高く、そして安全な依存関係の管理
Yarn はダウンロードしたパッケージをキャッシュするので、再ダウンロードは不要です。さらにリソースを最大限利用できるように処理を並列化するので、インストール時間はかつてないほど高速です。n