查看全局安装包
These days, most front-end projects are going to involve NPM packages of some kind. Occasionally, when browsing documentation for these packages, I’ll see a recommendation to install a package like this.
如今,大多数前端项目将涉及某种NPM软件包。 有时,在浏览这些软件包的文档时,我会看到建议安装这样的软件包的建议。
yarn global add <package>
Or like this.
或者像这样。
npm install --global <package>
In both of these examples, the package is installed globally. This means you can run the <package>
command from any directory on your system.
在这两个示例中,软件包都是全局安装的。 这意味着您可以从系统上的任何目录运行<package>
命令。
This works, but installing packages globally has a couple downsides.
这可行,但是在全球安装软件包有一些缺点。
- If you’re working with a team of developers, it’s hard to guarantee everyone is running the same package. 如果您与一组开发人员一起工作,则很难保证每个人都在运行相同的程序包。
- You can only have one version installed globally. This causes problems if you have different projects that rely on different versions of a package. 全局只能安装一个版本。 如果您有依赖于包的不同版本的不同项目,这将导致问题。
In this article, I’m going to show you three different approaches you can use to run packages without having to install them globally.
在本文中,我将向您展示三种不同的方法,您可以使用这些方法来运行软件包,而不必全局安装它们。
快速设置 (Quick Setup)
For this article, we’re going to install a small CLI tool called Figlet, which prints ASCII art text. Create an empty directory and navigate into it. Then add a package.json
file with the following:
在本文中,我们将安装一个名为Figlet的小型CLI工具,该工具可打印ASCII艺术文字。 创建一个空目录并进入该目录。 然后添加具有以下内容的package.json
文件:
{
"name": "example",
"license": "UNLICENSED",
"dependencies": {
"figlet-cli": "^0.1.0"
}
}
Run yarn install
or npm install
(depending on your preference) to install the package.
运行yarn install
或npm install
(取决于您的偏好)来安装软件包。
Note: The yarn
and npm
commands are identical from here on out, so I’m only going to list the yarn
versions.
注意:从现在开始, yarn
和npm
命令是相同的,因此我只列出yarn
版本。
编辑您的$ PATH (Editing Your $PATH)
The first way to run locally install packages as if they’re globally installed is by editing your $PATH
environment variable. The $PATH
variable tells your system which directories to look for executables in.
像本地安装软件包一样运行本地安装软件包的第一种方法是编辑$PATH
环境变量。 $PATH
变量告诉您的系统在哪个目录中查找可执行文件。
One of the handy features of Yarn and NPM is that they both include a .bin
directory inside of node_modules
that contains symbolic links to all of the installed executables. You can easily add this folder to your path. The trick here is to modify your $PATH
to include a local node_modules/.bin
directory. This will allow you to run any local NPM CLI tool as if it were installed globally.
Yarn和NPM的便捷功能之一是它们都在node_modules
内包含一个.bin
目录,该目录包含指向所有已安装的可执行文件的符号链接。 您可以轻松地将此文件夹添加到您的路径。 这里的技巧是修改$PATH
使其包含本地的 node_modules/.bin
目录。 这将使您可以运行任何本地NPM CLI工具,就像它是全局安装一样。
First, you need to determine which shell you’re running. To do that, you can type the following into your CLI.
首先,您需要确定正在运行哪个shell。 为此,您可以在CLI中键入以下内容。
echo $SHELL
If you haven’t configured a custom shell, this will likely be zsh
or bash
. If it’s bash
, open up the ~/.bash_profile
file. If it’s zsh
, open ~/.zshenv
. If the file you need doesn’t exist, then create it.
如果您尚未配置自定义外壳,则可能是zsh
或bash
。 如果是bash
,请打开~/.bash_profile
文件。 如果是zsh
,则打开~/.zshenv
。 如果所需文件不存在,请创建它。
Next, add the following to the bottom. Notice that ./node_modules/.bin
is a relative path. This means it’s appended to whatever directory you’re currently in.
接下来,将以下内容添加到底部。 请注意,。/ ./node_modules/.bin
是相对路径。 这意味着它将被附加到您当前所在的任何目录中。
export PATH="./node_modules/.bin:$PATH"
That’s it! Restart your shell, navigate into the directory you created, and try running figlet
.
而已! 重新启动您的Shell,导航到您创建的目录,然后尝试运行figlet
。
figlet Aww yeah
You should see something like this. Pretty neat, right?
您应该会看到类似这样的内容。 很整洁吧?
_ __ __ _
/ \__ ____ __ \ \ / /__ __ _| |__
/ _ \ \ /\ / /\ \ /\ / / \ V / _ \/ _` | '_ \
/ ___ \ V V / \ V V / | | __/ (_| | | | |
/_/ \_\_/\_/ \_/\_/ |_|\___|\__,_|_| |_|
纱线运行工具 (Running tools with Yarn)
Next up is defining commands in your package.json
. To add a command, all you have to do is add a scripts
section with your command name and what you’d like to run. In this example, I’ve added an aww-yeah
command.
接下来是在package.json
定义命令。 要添加命令,您要做的就是添加一个scripts
部分,其中包含您的命令名称和您要运行的内容。 在此示例中,我添加了aww-yeah
命令。
{
"name": "example",
"license": "UNLICENSED",
"dependencies": {
"figlet-cli": "^0.1.0"
},
"scripts": {
"aww-yeah": "figlet Aww Yeah"
}
}
You can run your custom command with yarn run <command>
. Most commands can also be shortened to yarn <command>
. Try it with yarn aww-yeah
!
您可以使用yarn run <command>
运行自定义命令。 大多数命令也可以简化为yarn <command>
。 尝试用yarn aww-yeah
!
You can even pass arguments to your custom commands. Try adding the ascii
command listed below to your scripts
and running yarn ascii Aww Yeah
.
您甚至可以将参数传递给自定义命令。 尝试将以下列出的ascii
命令添加到scripts
然后运行yarn ascii Aww Yeah
。
"scripts": {
"aww-yeah": "figlet Aww Yeah",
"ascii": "figlet"
}
Here’s a real-world example. I’m a big fan of both ESLint and Jest. Almost all of my projects have these commands defined in them.
这是一个真实的例子。 我是ESLint和Jest的 忠实拥护者 。 我的几乎所有项目都在其中定义了这些命令。
"scripts": {
"lint": "eslint --max-warnings=0 .",
"test": "jest"
}
This is great because my team and I can all share these commands. They’re also self-documenting, so if someone is new to a package they can glance at the package.json
to see which commands are available.
这很棒,因为我和我的团队可以共享这些命令。 它们也是自记录文件,因此,如果某人是某个软件包的新手,则可以浏览package.json
以查看哪些命令可用。
NPX (NPX)
Finally, we have NPX, a package runner by the folks from NPM. This handy tool allows you to run CLI commands without installing a package locally. This is great for tools that you only need to run once, such as generators.
最后,我们有NPX ,它是NPM的软件包运行程序。 这个方便的工具使您可以运行CLI命令, 而无需在本地安装软件包。 这对于只需要运行一次的工具(例如生成器)非常有用。
NPX is likely already installed on your machine if you’ve installed Node.js. If not you can install this one globally with yarn global add npx
.
如果已安装Node.js,则可能已在计算机上安装了NPX。 如果不是这样,您可以使用yarn global add npx
全局安装。
Let’s give it a shot with figlet
.
让我们用figlet
。
npx figlet Aww Yeah
Wasn’t that easy?
那不是那么容易吗?
Occasionally, you’ll run into a command that NPX doesn’t know how to find. An example is my Yeoman Generators repository. In those cases, you’ll need to tell NPX which package to run explicitly with a -p
flag.
有时,您会遇到NPX不知道如何查找的命令。 我的Yeoman Generators存储库就是一个例子。 在这些情况下,您需要使用-p
标志告诉NPX要显式运行哪个程序包。
npx -p yo -p @landonschropp/generator-eslint yo @landonschropp/eslint
全做完了! (All Done!)
And there you have it. Now, you can install any NPM module locally and run the command as if it were global. I personally use all three of these methods on a regular basis. I hope you find them as useful as I have!
那里有。 现在,您可以在本地安装任何NPM模块,然后像运行全局命令一样运行命令。 我个人定期使用这三种方法。 希望您发现它们像我一样有用!
翻译自: https://davidwalsh.name/stop-installing-packages-globally
查看全局安装包