LoginSignup
10
9

More than 3 years have passed since last update.

オフライン環境構築 npm編

Last updated at Posted at 2020-07-23

オフライン環境構築 目次へ

はじめに

【Yarn】パッケージをオフラインでインストールする という記事に触発されて、npmパッケージのオフラインインストール用のツールyarn-offlineを作った。

npm自体はオフラインインストールに対応していないのでyarnを使う。

ディレクトリ構成

ツールのディレクトリ構成はこんな感じ。

yarn-offline
├── .gitignore
├── .yarnrc
├── CHANGELOG.md
├── LICENSE
├── README.md
├── offline-cache
│   └── .gitkeep
└── package.json

ツールのミソは.yarnrc。プロジェクトディレクトリの.offline-cacheフォルダにtarボールを保存するための設定をしてある。

.yarnrc
yarn-offline-mirror "./offline-cache"
yarn-offline-mirror-pruning true

使い方

オンラインでの作業

まず、ツールをcloneする。

git clone https://github.com/kannkyo/yarn-offline
cd yarn-offline

次に、オンライン環境でyarnコマンドを実行する。

コマンド実行
yarn add <package_name>
# or
yarn add -D <package_name>

または、package.jsonに手入力で所望のパッケージを記載し、yarn online:downloadコマンドを実行する。

package.json
  "devDependencies": {
    "cross-env": "^7.0.2",
  },
  "dependencies": {
    "express": "^4.17.1"
  }
yarn online:download

これで、offline-cacheディレクトリにtarボールが保存される。

offline-cacheディレクトリ
offline-cache/
├── .gitkeep
├── @webassemblyjs-ast-1.9.0.tgz
...
└── yeoman-generator-2.0.5.tgz

オフラインでの作業

最後に、オフライン環境にyarn-offlineディレクトリを丸ごと移動してyarn offline:installコマンドを実行する。

yarn offline:install

これでOK。

バージョン更新方法

offline-cacheを作ってからしばらくたつと、パッケージのバージョンを上げたくなることがある。

こんな場合はyarn online:upgradeコマンドでバージョンアップが可能。

yarn online:upgrade
yarn online:download
10
9
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
10
9