Table of Contents
Guide Chapter
The meaning of "bao"
Use packages without using package manager
Problems solved by package manager
有些包管理器不是 npm
使 npm 成为“标准”包管理器的因素
关于 npx 的说明
接下来的步骤
Home Web Front-end CSS Tutorial What the Heck is a Package Manager?

What the Heck is a Package Manager?

Mar 15, 2025 am 09:59 AM

What the Heck is a Package Manager?

In this npm guide, we have a general understanding of npm – it stands for the Node Package Manager. In the process, we discuss the importance of the command line and how it works with npm.

We also specifically looked at "n" in npm - Node - and learned that Node is very similar to the JavaScript code we run a website in a browser. In fact, Node is JavaScript; it just runs outside the browser and can perform different operations than its browser-based counterpart.

Guide Chapter

  1. Who is this guide for?
  2. What does "npm" mean?
  3. What is the command line?
  4. What is Node?
  5. What is a package manager? (You are now!)
  6. How to install npm?
  7. How to install npm package?
  8. What is the npm command?
  9. How to install an existing npm project?

The meaning of "bao"

Now let's focus on the last two letters in npm, the "package manager" section. To fully understand what npm is, we need to know what a package manager is. So, naturally, in order to understand that , we need to understand what the "package" is.

" Package " is a general term for any external code file you add to your project and use in some way. Maybe you have used jQuery, Bootstrap, or Axios in your projects in the past. These are common examples of packages.

We call them "packages" because they are "packaged" and ready to use. Some languages ​​call them other names (for example, Ruby calls them "gem"), but the concept is the same. To avoid oversimplification, packages are code that you didn't write but get from some public sources for use in your project. You know, third-party code.

Or, if you prefer musical imitation using mnemonics:

? When you select the code ? Not yours, but you use ? Is this a package ? When you install something ? You import and call it, ? Is this a package

Packages are also often referred to as "dependencies" because the code you write depends on their existence. For example, code written with jQuery's $ will not work properly if jQuery itself is not loaded. (So, package managers are sometimes called "dependency managers".)

The size of the package may vary in terms of the amount of code included. A package can do some huge operations, thus changing the way you write your entire project (like the entire framework), or it can do some very small, focused operations that you just add where you need it (like widgets or helper programs for a specific task).

Use packages without using package manager

If you have used packages in the past, it is very likely that you are just using script tags in HTML to apply them to script tags extracted from external URLs (ideally from CDN). Here is how you can include jQuery in the HTML of your website:

<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
Copy after login

Another way is to download a copy of the package, add it to the folder of the project, and then link to it like so:

<script src="./js/jquery-3.6.0.min.js"></script>
Copy after login

Problems solved by package manager

Both methods have worked well over the years. It's very simple. It's very clean. As far as packages are concerned, it usually allows you to "set up and forget". So why do you need something else?

As you can imagine, owning a car may not seem attractive for someone who can easily use public transportation or do not need long trips. (This will be related to the package manager discussion, I promise. Stick with it.)

If you are convenient to use efficient public transportation, paying a high price for a large machine, you have to store it somewhere, regularly cleaning, maintaining and filling up expensive fuel, which may not be much of a benefit from your point of view. In this particular case, the benefits are insignificant; the cost is relatively overwhelming. People in this hypothetical position may even wonder why anyone wants a car!

I propose this analogy because when it solves problems you don't have , it can be very difficult to learn a new technology, which is very similar to buying a car that might not solve the transportation you already have. It might seem like a huge, unnecessary expense.

Then, the problems solved by the package manager are more about scale and processing problems. if only:

  • The number of projects you have is manageable;
  • The number of people working on the project is manageable;
  • The number of updates required to make to the package is manageable; and, most importantly,
  • Each package used in the project is client (browser) JavaScript or CSS.

The last one is the most important thing, because if you only run programs in your browser, you will never be able to use a lot of tools (more on that later).

If you do check all of these check boxes, you probably never go beyond this method. Your development method might look like this:

But even in this case, when you have multiple<script> 标签时,每个标签都链接到某个脚本或库的特定版本,那么<em>唯一可以查看您正在使用哪些包以及它们是否是最新的方法是手动打开 HTML 并查看代码。</script>

这本身并不是什么大问题,但随着项目规模和范围的扩大,这个问题呈指数级增长。您也许可以手动跟踪几个包,但是当我们谈论的是数百个——如果不是数千个——包时,您怎么可能做到这一点呢?即使您可以手动跟踪这些包,这仍然会带来人为错误的高风险。

HTML 的工作不是成为项目中所有使用的包的真相来源。 除了混合关注点外,在尝试合并团队成员之间不相关的作品时,它还可能导致冲突。

所有这些都很重要,但只是更大问题的一小部分。请了解,客户端 JavaScript 可能不是您永远想要包含在项目中的唯一类型的包,即使目前是这样——这就是事情真正开始崩溃的地方。

许多生产应用程序使用以下工具和包的某种组合,如果不是全部的话:

  • Sass(使编写 CSS 更容易)
  • PostCSS(增强 CSS 以实现最大的效率和兼容性)
  • Babel(转换较新的 JavaScript 以在较旧的浏览器中运行)
  • TypeScript(向 JavaScript 添加类型检查)
  • 由自动刷新浏览器以显示您的更改的开发服务器提供的热模块重新加载
  • 用于代码捆绑、缩小和/或连接的其他实用程序
  • 自动图像压缩
  • 测试库
  • 代码检查器

所有这些听起来都很棒——而且确实很棒!——但是请注意,您现在有多个依赖项,这些依赖项不仅不存在于您的 <script></script> 标签中,而且根本没有在您的项目中任何地方说明!任何人都无法知道——包括您未来的自己——使用了哪些工具或需要哪些工具才能使该项目运行。

即使您可以通过这种方式准确地知道项目需要什么,您仍然需要自己手动找到、下载和安装所有这些包。根据项目的情况,这很容易成为一天或更长时间的任务。

所有这一切都意味着您的工作流程现在看起来更像这样:

所有上述工具都非常方便,您仍然需要管理它们。 依赖项也是项目,它们发布更新以修复错误并引入新功能。因此,简单地在 HTML 中粘贴一个指向 CDN 上包的链接的 <script></script> 标签然后认为它已经完成是不现实的。您必须确保每件事不仅在您的机器上安装并正常工作,而且在每个协作者的机器上也正常工作。

包管理器存在是为了使项目的包——或依赖项——易于管理,方法是知道安装了什么、有什么可更新的以及一个包是否可能与另一个包冲突。包管理器的优点是它可以直接从命令行完成所有这些操作,并且工作量最少。

许多包管理器,尤其是 npm,还提供其他功能,这些功能可以打开更多可能性,从而使开发更高效。但是管理包是主要吸引力。

有些包管理器不是 npm

这部分与 npm 本身无关,但为了完整起见,我还应该提到 npm 不是唯一的 JavaScript 包管理器。例如,您可能会在代码示例中看到 Yarn 的引用。Yarn 和 npm 的工作方式非常相似,它们之间的大量互操作性是特意构建的。

有些人更喜欢一个包管理器而不是另一个包管理器。就我个人而言,我认为 npm 和 Yarn 之间的差异最初更为明显,但现在两者比以往任何时候都更加相似。

您可能会看到代码示例(包括 CSS-Tricks 文章中的一些示例),这些示例同时引用 yarn 和 npm。这是为了让读者知道这两种方法都可以,而不是需要同时使用它们。

Yarn 和 npm 的语法有时有所不同,但当只有一个存在时,通常可以轻松地将命令或项目从一个转换为另一个。从功能上讲,您使用哪一个很少(如果有的话)很重要——当然,除了所有在同一项目上一起工作的人都需要使用相同的包管理器以确保兼容性和一致性之外。

虽然 npm 和 Yarn 构成了开发人员使用的绝大多数包管理器,但还有一个名为 PnPm 的包管理器实际上是 npm,但性能更高、效率更高。权衡是 PnPm 在某些情况下需要更多技术知识,因此它更高级。

使 npm 成为“标准”包管理器的因素

同样,我只是提出其他包管理器是为了说明 npm 不是唯一存在的包管理器——但它通常是标准的。

是什么使它成为包管理器中的“标准”?包括 Ruby 和 PHP 在内的其他语言多年来都有包管理器;在 npm 之前,JavaScript 实际上没有任何好的包管理器。

npm 最初是一个独立的开源项目,但在 2020 年被微软收购。它技术上包含两个部分:实际的包管理器本身;以及包注册表,这是一个不断增长的列表,其中包含近 200 万个可供安装的包。

您可以将 npm 视为您可能想要在前端或基于 Node 的项目中使用的任何东西的应用程序商店。找到您想要的东西并通过命令行将其安装到您的系统。当发布新版本时,您可能会更新该包,或者如果项目不再依赖它,则完全删除它。

关于 npx 的说明

您也可能会看到 npx 命令出现。npx 实际上是 npm 的一部分,但是通过在命令中使用 npx 而不是 npm,您可以执行包的代码,而不会 永久 安装它。NPX 只安装它需要的东西,运行它,然后将其删除。

例如,如果您想运行安装程序脚本,这将很有用。npx 不需要下载安装程序,然后运行它,它允许您直接运行安装程序,之后不会在您的机器上留下任何东西。它就像一个清理自己东西的客人。

另一个很酷的例子:如果您只想编译项目中的 Sass 文件一次,而不必费心完全安装 Sass,您可以运行 npx sass(以及必要的输入和输出参数)。在大多数情况下,这可能不切实际,但如果您只是需要在这里和那里进行快速的一次性编译,那么 npx 将是一种方便的方法,因为它意味着需要更新和维护的已安装包更少。

接下来的步骤

好的,这就是我们称某个东西为包管理器时所指的深入探讨。对于 npm 而言,它专门用于安装和管理 Node 包,这些工具有助于向项目添加功能、添加方便的开发人员便利性……或所有上述内容!

接下来,我们将迈出使用 npm 的第一步。为此,我们需要将其安装到我们的系统中。这是本 npm 完全指南中的下一步。

← 第 4 章 第 6 章 →

The above is the detailed content of What the Heck is a Package Manager?. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Vue 3 Vue 3 Apr 02, 2025 pm 06:32 PM

It&#039;s out! Congrats to the Vue team for getting it done, I know it was a massive effort and a long time coming. All new docs, as well.

Building an Ethereum app using Redwood.js and Fauna Building an Ethereum app using Redwood.js and Fauna Mar 28, 2025 am 09:18 AM

With the recent climb of Bitcoin’s price over 20k $USD, and to it recently breaking 30k, I thought it’s worth taking a deep dive back into creating Ethereum

Can you get valid CSS property values from the browser? Can you get valid CSS property values from the browser? Apr 02, 2025 pm 06:17 PM

I had someone write in with this very legit question. Lea just blogged about how you can get valid CSS properties themselves from the browser. That&#039;s like this.

Stacked Cards with Sticky Positioning and a Dash of Sass Stacked Cards with Sticky Positioning and a Dash of Sass Apr 03, 2025 am 10:30 AM

The other day, I spotted this particularly lovely bit from Corey Ginnivan’s website where a collection of cards stack on top of one another as you scroll.

A bit on ci/cd A bit on ci/cd Apr 02, 2025 pm 06:21 PM

I&#039;d say "website" fits better than "mobile app" but I like this framing from Max Lynch:

Comparing Browsers for Responsive Design Comparing Browsers for Responsive Design Apr 02, 2025 pm 06:25 PM

There are a number of these desktop apps where the goal is showing your site at different dimensions all at the same time. So you can, for example, be writing

Using Markdown and Localization in the WordPress Block Editor Using Markdown and Localization in the WordPress Block Editor Apr 02, 2025 am 04:27 AM

If we need to show documentation to the user directly in the WordPress editor, what is the best way to do it?

Why are the purple slashed areas in the Flex layout mistakenly considered 'overflow space'? Why are the purple slashed areas in the Flex layout mistakenly considered 'overflow space'? Apr 05, 2025 pm 05:51 PM

Questions about purple slash areas in Flex layouts When using Flex layouts, you may encounter some confusing phenomena, such as in the developer tools (d...

See all articles