はじめに
フロントエンドフレームワークのドキュメントの中で登場するnpm create
。
どのような仕組みで動作しているか調べてみました。
本記事内ではAstroを例として挙げています
https://astro.build/
npm createの挙動
以下はhelpでnpm init
を確認したものだ。
こちらを確認すると、npm create
はnpm init
のエイリアスということがわかる。
❯ npm init --help
Create a package.json file
Usage:
npm init <package-spec> (same as `npx <package-spec>`)
npm init <@scope> (same as `npx <@scope>/create`)
Options:
[--init-author-name <name>] [--init-author-url <url>] [--init-license <license>]
[--init-module <module>] [--init-version <version>] [-y|--yes] [-f|--force]
[--scope <@scope>]
[-w|--workspace <workspace-name> [-w|--workspace <workspace-name> ...]]
[-ws|--workspaces] [--no-workspaces-update] [--include-workspace-root]
aliases: create, innit
Run "npm help init" for more info
詳細は以下ドキュメントで確認可能。
ドキュメントで確認すると、npm init
は以下のようにnpm exec
に変換されるようだ。
npm init foo
->npm exec create-foo
そのため、以下3つのコマンドの実行結果は同じとなる。
❯ npm create astro@latest
Need to install the following packages:
create-astro@4.10.0
Ok to proceed? (y)
❯ npm init astro@latest
Need to install the following packages:
create-astro@4.10.0
Ok to proceed? (y)
❯ npm exec create-astro@latest
Need to install the following packages:
create-astro@4.10.0
Ok to proceed? (y)
結論
npm create astro@latest
を実行すると内部ではnpm exec create-astro@latest
が実行される。