以下のページを参考に、create-blockで1つのプラグインに複数のブロックを追加してみようとしたときのことです。

[WP]create-block でブロックの追加とダイナミックブロックを作成する方法

@wordpress/create-block

ふむふむ、「create-block」で「–no-plugin」というオプションを付加すればできるのね、ということで、オプション付けて走らせてみたら...

error: unknown option ‘–no-plugin’

というエラーがパッと表示されただけでなしのつぶて。

エラー自体は「–no-plugin」というオプションは使えませんよ!というものなのですが、

npm --version

コマンドでnpmのバージョンを確認してみたら、npmのバージョンは9.6.2

公式ハンドブックによれば、

(node version 14.0.0 以上、npm version 6.14.4 以上が必要です)

となっていて問題ないはずなのに、なんで??となってしまいました。

ひとまず何でそうなるのかを調査。

create-blockのバージョンを確認してみる

肝心なのは、npmのバージョンではなく「create-block」のバージョン(冒頭の参考ページに書かれていました)だと思ったので、ひとまず以下のコマンドで確認。

npm view create-block version

すると、以下の数字(バージョン)が返ってきました。

1.0.2

え?バージョン1.0.2(驚)?

そこで試しに、以下のコマンドを使って、使えるoptionを表示してみると..

npx @wordpress/create-block --help
Options:
  -V, --version                output the version number
  -t, --template <name>        project template type name; allowed values: "static", "es5", the name of an external npm
                               package, or the path to a local directory (default: "static")
  --namespace <value>          internal namespace for the block name
  --title <value>              display title for the block and the WordPress plugin
  --short-description <value>  short description for the block and the WordPress plugin
  --category <name>            category name for the block
  --wp-scripts                 enable integration with `@wordpress/scripts` package
  --no-wp-scripts              disable integration with `@wordpress/scripts` package
  --wp-env                     enable integration with `@wordpress/env` package
  -h, --help                   output usage information

「–no-plugin」オプションないじゃん!という結果。これではエラーが出るはずです。

とはいえ、何でNodeやnpmのバージョンは大丈夫なのにダメなんだ?と若干頭から煙が...。

npmのキャッシュをクリアしたら解消できた

ここまで出てきたものをキーワードにしていろいろ調べていたら、npx(npm内の作業コマンド?)はインストール時にキャッシュされたものが使われるというような文献を発見!

それならキャッシュをクリアすればいいのでは?という結論に達しました。

そこで、以下のコマンドを使ってnpxをクリアしてみると..

C:\Users\□□□>npx clear-npx-cache

以下のような表示がされ、

Need to install the following packages:
  clear-npx-cache@1.0.1

しばらくすると以下の行が出たので「y」を入力

Ok to proceed? (y)

これで処理が完了し、キャッシュがクリアされたはず。

キャッシュがクリアできていれば、恐らく最近加わった「–no-plugin」が適用されるようになるだろうと、もう一度以下のコマンドでcreate-blockで使えるオプションの一覧を指示、

npx @wordpress/create-block --help

すると、

Need to install the following packages:
  @wordpress/create-block@4.13.0

が表示され、しばらく待っているとキャッシュクリアの時同様、以下のようなメッセージが出て、

Ok to proceed? (y)

再び「y」を入力すると以下のようになり、

Usage: wp-create-block [options] [slug]

最終的に、オプションの一覧が表示されました。

Options:
  -V, --version                output the version number
  -t, --template <name>        project template type name; allowed values: "standard", "es5", the name of an external
                               npm package, or the path to a local directory (default: "standard")
  --namespace <value>          internal namespace for the block name
  --title <value>              display title for the block and the WordPress plugin
  --short-description <value>  short description for the block and the WordPress plugin
  --category <name>            category name for the block
  --wp-scripts                 enable integration with `@wordpress/scripts` package
  --no-wp-scripts              disable integration with `@wordpress/scripts` package
  --wp-env                     enable integration with `@wordpress/env` package
  --no-plugin                  scaffold only block files
  --variant <variant>          the variant of the template to use
  -h, --help                   display help for command

Examples:
  $ wp-create-block
  $ wp-create-block todo-list
  $ wp-create-block todo-list --template es5 --title "TODO List"

問題は解消されたので、再度[WP]create-block でブロックの追加とダイナミックブロックを作成する方法]を参考にブロックを追加してみたら...無事追加できました!


この不具合?を調べるのにいろいろ検索したところ、とあるQ&Aサイトでは「Node.jsやnpmのことをもっと勉強してください」などという心ない?回答があったりしてげんなりし、完全に頭から煙が出てた時に出会った以下のページに感謝!!

npx で実行するコマンドが古かった時は、キャッシュをクリアしてあげると良い