简体   繁体   English

删除 React 中未使用的依赖项

[英]remove unused dependencies in React

How do I detect unused dependencies in a React project?如何检测 React 项目中未使用的依赖项? I know there are npm packages that can do it, but they only work with normal js projects, not with Reacts jsx file.我知道有 npm 包可以做到这一点,但它们只适用于普通的 js 项目,而不适用于 Reacts jsx 文件。 Is there a similar package that works with React?是否有适用于 React 的类似包?

try about depcheck ,Depcheck is a tool for analyzing the dependencies in a project to see: how each dependency is used, which dependencies are useless, and which dependencies are missing from package.json.尝试一下 depcheck ,Depcheck 是一个工具,用于分析项目中的依赖项,以查看:每个依赖项是如何使用的,哪些依赖项是无用的,以及 package.json 中缺少哪些依赖项。

NOTE: Depcheck requires installed node version > NODE 10注意:Depcheck 需要安装的节点版本 > NODE 10

you can use npm-check for finding outdated, incorrect, and unused dependencies in your project.您可以使用npm-check来查找项目中过时、不正确和未使用的依赖项。 After installing it you have to only run npm-check command in your terminal and it will list you status of your dependencies in your project安装后,您只需在终端中运行npm-check命令,它就会列出您项目中依赖项的状态

Using depcheck to Track Dependencies使用 depcheck 跟踪依赖关系

npm install -g depcheck

This installs depcheck globally on your system.这会在您的系统上全局安装 depcheck。

Run the following command inside your folder where you want your dependencies to be checked.要检查你的依赖你的文件夹运行以下命令。

depcheck

Ensure that this folder contains the package.json file, otherwise depcheck will not be able to track your dependencies.确保此文件夹包含 package.json 文件,否则depcheck 将无法跟踪您的依赖项。

Let suppose you installed the react-bootstrap package and did not import anything from it .假设您安装了react-bootstrap包并且没有从中导入任何东西

Running depcheck gives the following output:运行depcheck给出以下输出:

Unused dependencies
* ...
* react-bootstrap
* ... 
Missing dependencies
* ...  

In general, you can uninstall any npm package or dependency by running the following command:通常,您可以通过运行以下命令来卸载任何 npm 包或依赖项:

npm uninstall <package_name>
npm uninstall react-bootstrap

npm-uninstall docs npm 卸载文档

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM