如果你不再需要在代码中使用包,我们建议将其卸载并将其从项目的依赖中删除。

🌐 If you no longer need to use a package in your code, we recommend uninstalling it and removing it from your project's dependencies.

卸载本地包

🌐 Uninstalling local packages

从 node_modules 目录中删除本地包

🌐 Removing a local package from your node_modules directory

要从你的 node_modules 目录中移除一个包,可以在命令行中使用 uninstall 命令。如果该包有作用域,请包括作用域。

🌐 To remove a package from your node_modules directory, on the command line, use the uninstall command. Include the scope if the package is scoped.

这将卸载一个包,完全删除代表它安装的所有 npm。

🌐 This uninstalls a package, completely removing everything npm installed on its behalf.

它还会从 package.json 中的 dependencies、devDependencies、optionalDependencies 和 peerDependencies 对象中删除包。

🌐 It also removes the package from the dependencies, devDependencies, optionalDependencies, and peerDependencies objects in your package.json.

此外,如果你有 npm-shrinkwrap.json 或 package-lock.json,npm 也会更新这些文件。

🌐 Further, if you have an npm-shrinkwrap.json or package-lock.json, npm will update those files as well.

无范围的包

🌐 Unscoped package

npm uninstall <package_name>

范围包

🌐 Scoped package

npm uninstall <@scope/package_name>

示例

🌐 Example

npm uninstall lodash

删除本地包而不从 package.json 中删除它

🌐 Removing a local package without removing it from package.json

使用 --no-save 将告诉 npm 不要从你的 package.jsonnpm-shrinkwrap.jsonpackage-lock.json 文件中移除该包。

🌐 Using the --no-save will tell npm not to remove the package from your package.json, npm-shrinkwrap.json, or package-lock.json files.

示例

🌐 Example

npm uninstall --no-save lodash

--save-S 会告诉 npm 从你的 package.jsonnpm-shrinkwrap.jsonpackage-lock.json 文件中移除该包。这是默认行为,但如果你的 .npmrc 文件中有例如 save=false,你可能需要使用它。

确认本地包卸载

🌐 Confirming local package uninstallation

要确认 npm uninstall 是否正常工作,请检查 node_modules 目录中是否不再包含已卸载软件包的目录。

🌐 To confirm that npm uninstall worked correctly, check that the node_modules directory no longer contains a directory for the uninstalled package(s).

  • Unix 系统(例如 macOS):ls node_modules
  • Windows 系统:dir node_modules

卸载全局包

🌐 Uninstalling global packages

要卸载未加范围的全局包,请在命令行中使用带有 -g 标志的 uninstall 命令。如果包是有范围的,请包含该范围。

🌐 To uninstall an unscoped global package, on the command line, use the uninstall command with the -g flag. Include the scope if the package is scoped.

无范围的包

🌐 Unscoped package

npm uninstall -g <package_name>

范围包

🌐 Scoped package

npm uninstall -g <@scope/package_name>

示例

🌐 Example

例如,要卸载名为 jshint 的软件包,请运行:

🌐 For example, to uninstall a package called jshint, run:

npm uninstall -g jshint

资源

🌐 Resources

卸载本地包

🌐 Uninstalling local packages

卸载全局包

🌐 Uninstalling global packages