0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

はじめに

ドキュメントのコピペや、AIに言われるがままに環境構築をしていると、プロジェクトによってアプリケーションの起動コマンドが npm run dev だったり npm run start だったりと、同じことをしてそうなのにコマンドが異なる時があります。

この記事では、npm run から始まるコマンドが何を実行しているのか、どこで実行内容が設定されているのかについてまとめます。

npm run ○○は何を実行するのか

npm run <script> は、package.jsonscripts に定義されているコマンドを実行します。
たとえば、次のような package.json があるとします。

package.json
{
  "scripts": {
    "dev": "next dev",
    "build": "next build",
    "start": "next start"
  }
}

この場合、npm run dev を実行すると実際には next dev を実行します。
そして npm run start を実行すると next start を、npm run build を実行すると next build を実行します。

要するに、scriptsはプロジェクトごとに自由に設定できるので、プロジェクトによってアプリケーション起動のコマンドが違っていても、scriptsの中身を見ると実際には同じことをしている場合もあるのですね。

npm run startはrunを省略できる

npm run start だけは、npm start と省略して実行できます。
npm公式ドキュメントでは次のように説明されています。

This runs a predefined command specified in the "start" property of a package's "scripts" object.

出典: npm-start | npm Docs

また、scripts.start が定義されていない場合、npmはデフォルトで node server.js を実行します。

フレームワークによって慣習が異なる

scriptsの中身は、フレームワーク等によって慣習・公式ドキュメントに書かれている内容が異なります。
たとえばNext.jsの公式ドキュメントでは、package.json に次のようなscriptsを追加する例が載っています。

package.json
{
  "scripts": {
    "dev": "next dev",
    "build": "next build",
    "start": "next start",
    "lint": "eslint",
    "lint:fix": "eslint --fix"
  }
}

出典: Getting Started: Installation | Next.js

一方で、NestJSでは、次のようなscriptsが定義されています。

package.json
{
  "scripts": {
    "build": "nest build",
    "start": "nest start",
    "start:dev": "nest start --watch",
    "start:debug": "nest start --debug --watch",
    "start:prod": "node dist/main"
  }
}

出典: nestjs/typescript-starter - package.json

当然個人での開発以外ではscriptsの内容は結局そのプロジェクト次第なので、最終的には、package.json の中身やREADMEを確認する必要があります。

まとめ

  • npm run <script> は、package.jsonscriptsに定義されたコマンドを実行する
  • startnpm start と省略して実行できる

お読みいただきありがとうございました。

参考文献

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?