查看全局安装包_停止全局安装软件包

查看全局安装包

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 installnpm install (取决于您的偏好)来安装软件包。

Note: The yarn and npm commands are identical from here on out, so I’m only going to list the yarn versions.

注意:从现在开始, yarnnpm命令是相同的,因此我只列出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.

如果您尚未配置自定义外壳,则可能是zshbash 。 如果是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.

这是一个真实的例子。 我是ESLintJest的 忠实拥护者 。 我的几乎所有项目都在其中定义了这些命令。

"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

查看全局安装包

Python开发中,安装软件包时可能会因为依赖项问题导致安装失败。为了解决和检查依赖项问题,你可以按照以下步骤进行操作: 1. 仔细阅读错误信息:通常Python安装软件包失败时会在控制台输出错误信息,这些信息会告诉你缺少哪个具体的依赖项或版本不兼容等问题。 2. 检查依赖项版本:确保你的Python环境中的依赖项版本符合所尝试安装的包的要求。你可以使用如下命令查看安装的包及其版本: ``` pip list ``` 或者对于某些包,你可能需要查看包内的`requirements.txt`文件或官方文档来获取依赖项及其版本信息。 3. 使用虚拟环境:为了避免全局环境下的依赖冲突,推荐使用虚拟环境来安装和管理Python包。可以使用`virtualenv`或`conda`创建独立的环境。 4. 使用`--no-deps`选项:在使用`pip`安装软件包时,可以添加`--no-deps`选项来避免安装依赖,这样可以检查你的环境中是否有合适的依赖项已经存在: ``` pip install some-package --no-deps ``` 5. 更新pip:确保你使用的pip是最新版本,旧版本的pip可能无法正确处理依赖关系。你可以使用以下命令来升级pip: ``` pip install --upgrade pip ``` 6. 使用其他工具:除了pip之外,还可以使用如`conda`或`easy_install`等工具来安装包,有时候这些工具可以提供不同的依赖解决方案。 7. 手动安装依赖:如果上述方法都失败,你可以尝试手动下载和安装依赖项,然后再尝试安装原来的包。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值