package.json の dependencies と devDependencies の違い
Node.js と npm で devDependencies がインストールされない問題
Node.js プロジェクトで npm install
を実行しても devDependencies
がインストールされない場合があり、開発環境で必要な依存関係が不足して問題が発生する可能性があります。
原因
npm install
はデフォルトで dependencies
に記載されているパッケージのみをインストールします。devDependencies
は開発環境でのみ使用されるパッケージを指定するため、別途インストールする必要があります。
解決策
devDependencies
をインストールするには以下の方法があります。
--dev オプションを使用する
npm install --dev
このコマンドは dependencies
と devDependencies
両方のパッケージをインストールします。
npm install --only=dev
package.json
ファイルの dependencies
セクションに devDependencies
を移動します。
npm-run-all
のようなツールを使用して、devDependencies
をインストールするスクリプトを作成できます。
補足
- 上記の方法以外にも、環境設定ファイルや Yarn
などのツールを使用して
devDependencies
をインストールすることもできます。 devDependencies
は開発環境でのみ使用されるため、本番環境にデプロイする際には不要です。
注意
npm install --only=dev
を使用すると、dependencies
に記載されているパッケージはインストールされません。
用語解説
- Node.js: JavaScript をサーバーサイドで実行するためのプラットフォーム
- npm: Node.js 用のパッケージ管理ツール
- dependencies: アプリケーション実行時に必要なパッケージ
- devDependencies: 開発環境でのみ使用されるパッケージ
// package.json
{
"name": "my-project",
"version": "1.0.0",
"description": "My project",
"main": "index.js",
"scripts": {
"start": "node index.js",
"test": "mocha test"
},
"keywords": ["node", "javascript"],
"author": "Your Name",
"license": "ISC",
"dependencies": {
"express": "^4.17.1"
},
"devDependencies": {
"mocha": "^8.4.0",
"chai": "^4.3.4"
}
}
// index.js
const express = require('express');
const app = express();
app.get('/', (req, res) => {
res.send('Hello World!');
});
app.listen(3000, () => {
console.log('Example app listening on port 3000!');
});
// test/test.js
const assert = require('chai').assert;
describe('My Test', () => {
it('should return Hello World!', () => {
const res = {
send: (text) => {
assert.equal(text, 'Hello World!');
}
};
app.get('/', (req, res) => {
res.send('Hello World!');
});
app.get('/', (req, res) => {
res.send('Hello World!');
});
});
});
上記コードを実行するには、以下のコマンドを実行します。
npm install
実行結果
$ npm install
> [email protected] install /Users/your-name/my-project
> node-pre-gyp install --fallback-to-build
> node-pre-gyp WARN Using request for HTTP requests (Sync https://github.com/request/request/issues/3142)
> node-pre-gyp WARN Pre-built binaries not found for [email protected] and [email protected] (node-v16.14.0-linux-x64) on npm WARN This is probably not a problem with npm itself, and is likely due to the package author not publishing pre-built binaries.
> node-pre-gyp WARN If you're the package author and would like to publish pre-built binaries, please refer to https://github.com/node-pre-gyp/node-pre-gyp#readme
> node-pre-gyp WARN To view the stack trace of this warning, add `--loglevel=verbose` to your command.
> [email protected] installed as a devDependency
+ [email protected]
added 17 packages from 14 contributors, updated 1 package and audited 17 packages in 10.21s
found 0 vulnerabilities (0 low, 0 moderate, 0 high)
1 package is looking for funding
run `npm fund` for details
npm list --dev
このコマンドを実行すると、devDependencies
に記載されているパッケージの一覧が表示されます。
$ npm list --dev
├─┬ [email protected]
│ ├─┬ [email protected]
│ │ └── [email protected]
│ └── [email protected]
├── [email protected]
│ ├── @mocha/[email protected]
│ ├── @mocha/[email protected]
│ ├── @mocha/[email protected]
│ ├── @mocha/[email protected]
│ ├── @mocha/[email protected]
│ ├── @mocha/[email protected]
│ ├── @mocha/[email protected]
│ ├── [email protected]
│ ├── [email protected]
│
npm install 以外で devDependencies をインストールする方法
npm install --only=dev
Yarn を使用する
Yarn は npm
と同様のパッケージ管理ツールですが、devDependencies
を自動的にインストールする機能があります。
--only=dev
オプションは、devDependencies
のみをインストールするオプションです。このオプションを使用すると、dependencies
に記載されているパッケージはインストールされません。
- package.json を編集する
package.json
ファイルの dependencies
セクションに devDependencies
を移動すると、npm install
を実行するたびに devDependencies
が自動的にインストールされます。
- npm-run-all を使用する
Yarn は npm
と同様のパッケージ管理ツールですが、devDependencies
を自動的にインストールする機能があります。Yarn を使用してプロジェクトを初期化する場合は、devDependencies
を別途インストールする必要はありません。
例
// package.json
{
"name": "my-project",
"version": "1.0.0",
"description": "My project",
"main": "index.js",
"scripts": {
"start": "node index.js",
"test": "mocha test",
"dev": "npm-run-all --parallel lint test"
},
"keywords": ["node", "javascript"],
"author": "Your Name",
"license": "ISC",
"dependencies": {
"express": "^4.17.1"
},
"devDependencies": {
"mocha": "^8.4.0",
"chai": "^4.3.4",
"eslint": "^8.12.0"
}
}
上記コードの場合、npm run dev
コマンドを実行すると、eslint
によるコードチェックと mocha
によるテストが同時に実行されます。
yarn install
npm install --dev
オプション以外にも、devDependencies
をインストールする方法はいくつかあります。それぞれの方法の特徴を理解して、プロジェクトに合った方法を選択してください。
node.js npm