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 thenode_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.
在您的命令行上。
npm包全局本地路径