LoginSignup
0
1

More than 1 year has passed since last update.

【npm】npm installできないようにする【yarn】【pnpm】

Last updated at Posted at 2022-05-01

はじめに

本記事はプログラミング初学者が学習を進めていて疑問に思った点について調べた結果を備忘録も兼ねてまとめたものです。
そのため、記事の内容に誤りが含まれている可能性があります。ご容赦ください。
間違いを見つけた方は、お手数ですが、ご指摘いただけますと幸いです。
また、以下の記事の一環として記載しますが、yarnやpnpmを使用する際にnpm installを不可にする方法はDockerを使用しない場合でも同様です。

npm installを使用不可にする

npm installを使用不可にする方法はいくつかあります。
以下yarnを使用する前提で記述します。

only-allowを使用する

only-allowを使用することでyarnの使用を強制できます。
使用するにはpackage.jsonに以下のとおり追記します。

package.json
{
  "scripts": {
    "preinstall": "npx -y only-allow yarn"
  }
}

npxに-yをつけていますが、つけている理由については以下をご参照ください。

しかし、only-allownpm installでは以下のようなエラーが出るものの、npm install <package>ではエラーが出ませんでした。

これは、GitHubのissueでも言及されています。

ターミナル
$ npm install

> myapp-front@0.1.0 preinstall
> npx -y only-allow yarn

╔═════════════════════════════════════════════════════════════╗
║                                                             ║
║   Use "yarn" for installation in this project.              ║
║                                                             ║
║   If you don't have Yarn, install it via "npm i -g yarn".   ║
║   For more details, go to https://yarnpkg.com/              ║
║                                                             ║
╚═════════════════════════════════════════════════════════════╝
npm ERR! code 1
npm ERR! path /Users/user_name/Documents/sources/docker-next-test/Next
npm ERR! command failed
npm ERR! command sh -c  npx -y only-allow yarn

npm ERR! A complete log of this run can be found in:
npm ERR!     /Users/user_name/.npm/_logs/2022-04-28T14_44_01_788Z-debug-0.log
ターミナル
$  npm install uuid

added 1 package, and audited 234 packages in 575ms

67 packages are looking for funding
  run `npm fund` for details

found 0 vulnerabilities

npm install <package>でもエラーが出るようにしたいです。
only-allowはpnpmなども使用不可にできる点で優れていますが、他の方法でnpmのみ使用不可にします。

.npmrcを作成する

まず、package.jsonに以下を追記します。

package.json
{

  "engines": {
    "npm": "npmではなくyarnを使用してください"
  },

}

次に、.npmrcを作成します。

.npmrc
engine-strict=true

これで、npmを使用するとエラーが出るようになります。

ターミナル
$ npm install
npm ERR! code EBADENGINE
npm ERR! engine Unsupported engine
npm ERR! engine Not compatible with your version of node/npm: myapp-front@0.1.0
npm ERR! notsup Not compatible with your version of node/npm: myapp-front@0.1.0
npm ERR! notsup Required: {"npm":"npmではなくyarnを使用してください"}
npm ERR! notsup Actual:   {"npm":"8.3.1","node":"v16.14.0"}

npm ERR! A complete log of this run can be found in:
npm ERR!     /Users/user_name/.npm/_logs/2022-04-28T18_14_01_861Z-debug-0.log
ターミナル
$ npm install <package>
npm ERR! code EBADENGINE
npm ERR! engine Unsupported engine
npm ERR! engine Not compatible with your version of node/npm: myapp-front@0.1.0
npm ERR! notsup Not compatible with your version of node/npm: myapp-front@0.1.0
npm ERR! notsup Required: {"npm":"npmではなくyarnを使用してください"}
npm ERR! notsup Actual:   {"npm":"8.3.1","node":"v16.14.0"}

npm ERR! A complete log of this run can be found in:
npm ERR!     /Users/user_name/.npm/_logs/2022-04-28T16_47_26_621Z-debug-0.log

.npmrcによってnpmを使用した際に--engine-strictオプションが付くようになります。
するとpackage.jsonで指定したnpmのバージョン以外は使用不可になります。
本来npmのバージョンを記述する箇所に「npmではなくyarnを使用してください」と記述しているため必ずエラーとなります。
この「npmではなくyarnを使用してください」の箇所はnpmのバージョンに存在しないものであれば何でも構いません。

Corepackを利用する

yarnの使用を強制するためにCorepackを利用する手段もあります。
以下をご参照ください。

0
1
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
0
1