LoginSignup
13
10

More than 5 years have passed since last update.

npm installを無効にする

Last updated at Posted at 2018-03-10

yarnを使ってるプロジェクトで、開発者がうっかり npm install でパッケージのインストールをしてしまうと依存性解決の差異でよくわからない問題がおきることがあります。

こういう場合の対策の仕組みとして、以下のようなスクリプトを用意して npm install を無効化し、 yarn install を使うよう誘導することができます。

tools/preinstall-script.js
/**
 * Do NOT allow using `npm` as package manager.
 */
if (process.env.npm_execpath.indexOf('yarn') === -1) {
  console.error('You must use Yarn to install dependencies:');
  console.error('  $ yarn install');
  process.exit(1);
}

作成したスクリプトは preinstall で実行させます。

package.json
  "scripts": {
    "preinstall": "node tools/preinstall-script.js"
  }

この仕組みはAMP HTMLでも採用されているようです。

参考: https://github.com/yarnpkg/yarn/issues/4895

13
10
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
13
10