composer.json中的require-dev部分是什麼?
require-dev在composer.json中用於列出僅在開發環境中需要的PHP包,如測試框架、代碼質量工具和調試器,這些不在生產環境中安裝。 1. 常見類型包括PHPUnit、PHPStan、Xdebug等。 2. 默認情況下composer install會安裝require和require-dev包,但生產環境應使用--no-dev標誌跳過dev依賴。 3. 使用composer require --dev可將包添加到require-dev。 4. 僅在開發或測試中需要的包應放在此處,而生產所需功能的包應放在require中。 5. 保持require-dev簡潔,不活躍使用的工具應刪除,CI/CD通常會安裝dev依賴。
If you've ever looked at a composer.json
file and wondered what the require-dev
section is for, you're not alone. In short, it's where you list PHP packages that are only needed in development environments — not in production.
This means tools like testing frameworks, code sniffers, debuggers, or anything else you use while building or maintaining the project doesn't get installed when the app goes live.
What kind of packages go in require-dev?
The main idea behind require-dev
is to separate development-specific dependencies from those required for the application to run in production.
Here are some common types of packages you'll find in this section:
- Testing tools : PHPUnit, Pest, Codeception
- Code quality tools : PHPStan, Psalm, PHP_CodeSniffer
- Debugging tools : Xdebug, Laravel Debugbar (in some cases)
- Development helpers : PHP CS Fixer, Rector, Faker (sometimes)
These tools help developers write better code, test functionality, and catch issues early — but they don't need to be present once the app is deployed.
How does require-dev affect Composer commands?
When you run composer install
, by default, Composer installs both require
and require-dev
packages — unless you tell it otherwise.
In production, you usually want to skip installing dev dependencies. To do that, you add the --no-dev
flag:
composer install --no-dev
This makes your production environment lighter and more secure by not including unnecessary packages.
Also, when you add a package using composer require
, it goes into require
by default. If you want it in require-dev
, you need to specify it:
composer require --dev phpunit/phpunit
When should you use require-dev?
You should use require-dev
whenever a package isn't needed outside of development or testing.
For example:
- You're writing unit tests with PHPUnit → definitely a dev dependency.
- You're using Laravel Dusk for browser testing → dev only.
- You have a package that generates documentation → dev use only.
However, if a package adds functionality that's needed in production — like a library that handles payments or sends emails — it belongs in the regular require
block.
One thing to note: if you're building a package or framework meant to be used by others, avoid putting optional tools in require
. Instead, suggest them via "suggest"
or put them in require-dev
.
A few small but important details
- The order of packages in
require-dev
doesn't matter — Composer doesn't care about sorting. - It's good practice to keep
require-dev
clean. If a tool isn't actively used, remove it. - CI/CD pipelines often install dev dependencies because they run tests and linters.
Also, if you're sharing your package with others, remember: people consuming your package won't get your require-dev
dependencies automatically — which is correct behavior.
So that's basically it. require-dev
is just a way to keep your development tools organized and out of production. Not complicated, but easy to misuse if you're not paying attention.
以上是composer.json中的require-dev部分是什麼?的詳細內容。更多資訊請關注PHP中文網其他相關文章!

熱AI工具

Undress AI Tool
免費脫衣圖片

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

記事本++7.3.1
好用且免費的程式碼編輯器

SublimeText3漢化版
中文版,非常好用

禪工作室 13.0.1
強大的PHP整合開發環境

Dreamweaver CS6
視覺化網頁開發工具

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)

TouseaprivateComposerrepository,configurecomposer.jsonwiththecorrectrepositoryURL,handleauthenticationsecurelyviaSSHorHTTPS,andensurepackagesareaccessible.First,addtherepositoryincomposer.jsonusingeitheraVCStypeforGitrepositoriesoraComposertypeforpri

checkphpinstallation byrunningphp-vincommand promptandensurephpisinpath.2.DownloadtheLoadtheComposer-Setup.exeinstallererfromgetComposer.org,runit,lunit,lastthewizard,andallowsystemystem-stalmystem-wideinstallation.3.verifyinstallationbyinstallationbyrientbyrimentbyrunningcomposer-vermandcommandinananeanenanewcommptttttttrompttromptttrompttrompttrompt

要配置Composer的classmap自動加載,首先在composer.json中使用"autoload"下的"classmap"鍵指定目錄或文件。例如:{"autoload":{"classmap":["lib/","database/models/"]}},Composer會掃描這些路徑中的.php文件並生成類映射。也可指定單個文件如legacy_class.php。更新

Composer'sauditcommandchecksforknownsecurityvulnerabilitiesinPHPprojectdependenciesbyscanningthecomposer.lockfile.1.Itcross-referencespackageversionsagainstvulnerabilitydatabaseslikeGitHubAdvisoryDatabase.2.Itreportsissuessuchasremotecodeexecution,XS

Composerclearcache命令用於清除本地緩存數據以解決包版本過時或依賴問題,其核心作用是刪除存儲的包元數據、下載歸檔和Git克隆等信息。 1.它不會影響vendor目錄或composer.lock文件;2.可選擇性清除特定緩存類型如包文件、倉庫元數據、VCS克隆;3.緩存位置因係統而異,默認位於Linux/macOS的~/.composer/cache或Windows的AppData\Local\Composer;4.若使用Docker或Homestead需確認是否在正確環境中執行;5

thequiresectionInacomposer.jsonfileisusedtodefineTheexternalPackages andTheIrversionsThatArenenceSareReassaryForphpropprojectTorun.itenseSuresthiseDependendendenciesareSareSareSareSareSareDviaComposerWhenRuminnRuminnRuminnrunningcomposerinstall.eachentryfollowsthellyfollowStheTheeforlstheTheeforsTheorsTheeor/vendor/vendor/poffent'vendor/poffent':“:”:“:”:“:”:“:”:“:

composer.json是PHP項目中使用Composer所必需的核心配置文件,用於定義依賴、版本、自動加載等設置。它通過name、description、require、require-dev、autoload和scripts等關鍵字段明確項目信息與需求,並可通過composerinit或手動創建生成,也可通過Composer命令如composerrequire自動更新。該文件確保團隊成員使用一致的庫和版本,支持自動加載機制,簡化依賴管理與項目共享,是構建可維護、可部署PHP項目的基石。

使用composerremove命令卸載PHP項目中的包,該命令會從composer.json的require或require-dev中移除指定包,並自動調整依賴關係。 1.執行composerremovevendor/package從require移除;2.使用--dev參數可從require-dev移除;3.Composer會自動更新依賴並重建自動加載器;4.可運行composerinstall及檢查vendor/目錄確保徹底清理;5.最後提交版本控制更改以保存修改。
