search
HomeWeb Front-endCSS TutorialWhat the Heck is a Package Manager?

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>

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>

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
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
Where should 'Subscribe to Podcast' link to?Where should 'Subscribe to Podcast' link to?Apr 16, 2025 pm 12:04 PM

For a while, iTunes was the big dog in podcasting, so if you linked "Subscribe to Podcast" to like:

Browser Engine DiversityBrowser Engine DiversityApr 16, 2025 pm 12:02 PM

We lost Opera when they went Chrome in 2013. Same deal with Edge when it also went Chrome earlier this year. Mike Taylor called these changes a "Decreasingly

UX Considerations for Web SharingUX Considerations for Web SharingApr 16, 2025 am 11:59 AM

From trashy clickbait sites to the most august of publications, share buttons have long been ubiquitous across the web. And yet it is arguable that these

Weekly Platform News: Apple Deploys Web Components, Progressive HTML Rendering, Self-Hosting Critical ResourcesWeekly Platform News: Apple Deploys Web Components, Progressive HTML Rendering, Self-Hosting Critical ResourcesApr 16, 2025 am 11:55 AM

In this week's roundup, Apple gets into web components, how Instagram is insta-loading scripts, and some food for thought for self-hosting critical resources.

Git Pathspecs and How to Use ThemGit Pathspecs and How to Use ThemApr 16, 2025 am 11:53 AM

When I was looking through the documentation of git commands, I noticed that many of them had an option for . I initially thought that this was just a

A Color Picker for Product ImagesA Color Picker for Product ImagesApr 16, 2025 am 11:49 AM

Sounds kind of like a hard problem doesn't it? We often don't have product shots in thousands of colors, such that we can flip out the with . Nor do we

A Dark Mode Toggle with React and ThemeProviderA Dark Mode Toggle with React and ThemeProviderApr 16, 2025 am 11:46 AM

I like when websites have a dark mode option. Dark mode makes web pages easier for me to read and helps my eyes feel more relaxed. Many websites, including

Some Hands-On with the HTML Dialog ElementSome Hands-On with the HTML Dialog ElementApr 16, 2025 am 11:33 AM

This is me looking at the HTML element for the first time. I've been aware of it for a while, but haven't taken it for a spin yet. It has some pretty cool and

See all articles

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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Chat Commands and How to Use Them
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version