npm包全局本地路径_npm全局或本地软件包

npm包全局本地路径

The main difference between local and global packages is this:

本地和全局软件包之间的主要区别是:

  • local packages are installed in the directory where you run npm install <package-name>, and they are put in the node_modules folder under this directory

    本地软件包安装在运行npm install <package-name>的目录中,并将它们放在此目录下的node_modules文件夹中

  • global packages are all put in a single place in your system (exactly where depends on your setup), regardless of where you run npm install -g <package-name>

    全局软件包都放在系统中的单个位置(确切的位置取决于您的设置),无论您在何处运行npm install -g <package-name>

In your code, they are both required in the same way:

在您的代码中,它们都以相同的方式被要求:

require('package-name')

so when should you install in one way or another?

所以什么时候应该以一种或另一种方式安装?

In general, all packages should be installed locally.

通常, 所有软件包都应在本地安装

This makes sure you can have dozens of applications in your computer, all running a different version of each package if needed.

这样可以确保您的计算机中可以有数十个应用程序,并且在需要时全部运行不同版本的每个程序包。

Updating a global package would make all your projects use the new release, and as you can imagine this might cause nightmares in terms of maintenance, as some packages might break compatibility with further dependencies, and so on.

更新全局软件包将使您的所有项目都使用新版本,并且您可以想象,这可能会导致维护方面的噩梦,因为某些软件包可能会破坏与其他依赖项的兼容性,依此类推。

All projects have their own local version of a package, even if this might appear like a waste of resources, it’s minimal compared to the possible negative consequences.

所有项目都有自己的软件包本地版本,即使这看起来像浪费资源,与可能产生的负面影响相比也很小。

A package should be installed globally when it provides an executable command that you run from the shell (CLI), and it’s reused across projects.

当程序包提供从外壳程序(CLI)运行的可执行命令时, 应在全局安装该程序包,并且该程序包在项目中重复使用。

You can also install executable commands locally and run them using npx, but some packages are just better installed globally.

您还可以在本地安装可执行命令并使用npx运行它们,但是某些软件包最好在全局安装。

Great examples of popular global packages which you might know are

您可能知道的流行全球软件包的绝佳示例是

  • npm

    npm

  • create-react-app

    create-react-app

  • vue-cli

    vue-cli

  • grunt-cli

    grunt-cli

  • mocha

    mocha

  • react-native-cli

    react-native-cli

  • gatsby-cli

    gatsby-cli

  • forever

    forever

  • nodemon

    nodemon

You probably have some packages installed globally already on your system. You can see them by running

您可能已经在系统上全局安装了一些软件包。 您可以通过运行查看它们

npm list -g --depth 0

on your command line.

在您的命令行上。

翻译自: https://flaviocopes.com/npm-packages-local-global/

npm包全局本地路径

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值