npm 包管理器

by Kevin Kononenko

凯文·科诺年科(Kevin Kononenko)

If you understand the general way that Hollywood movies are made, then you can understand Node Package Manager (NPM).

如果您了解好莱坞电影的一般制作方法,那么您可以了解Node Package Manager(NPM)。

Did you know that the initial version of Node.js was written by just one programmer, Ryan Dahl, in 2009?

您是否知道Node.js的初始版本仅由一位程序员Ryan Dahl在2009年编写?

Today, in 2018, millions of developers have used Node.js to create the back-end for their web applications. But, Node has relied on an active open-source community to build out many specialized packages within the Node Package Manager, or NPM.

今天,在2018年,数百万开发人员已经使用Node.js为他们的Web应用程序创建后端。 但是,Node依靠活跃的开源社区在Node Package Manager或NPM中构建了许多专门的软件包。

There are two reasons why Ryan Dahl did not develop all of these specialized functions himself:

Ryan Dahl自己未开发所有这些专门功能的原因有两个:

  1. Developers would not want to use a massive, clunky framework that was written to cover hundreds of use cases.开发人员不想使用庞大的,笨拙的框架,该框架旨在涵盖数百个用例。
  2. It would take too damn long to create all the specialized packages yourself.自己创建所有专用软件包可能要花很长时间。

Instead, Ryan knew that if Node grew in popularity, developers would be willing to contribute. So, he launched NPM in 2010 to organize all these packages created by the community.

相反,Ryan知道,如果Node越来越流行,那么开发人员将愿意做出贡献。 因此,他在2010年启动了NPM,以组织社区创建的所有这些软件包。

However, as a beginner web developer, it can be challenging to understand this whole ecosystem, and how to access it on your local computer.

但是,作为一名初学者Web开发人员,要理解整个生态系统以及如何在本地计算机上访问它可能是一个挑战。

After thinking on it for awhile, I realized that using NPM is kind of like being the director of a Hollywood movie. It’s your job to juggle a bunch of people (or packages) with specialized functions without making everyone crazy (or making it impossible to build your app).

经过一段时间的思考,我意识到使用NPM就像是好莱坞电影的导演。 您需要做的是使一群具有特殊功能的人(或程序包)杂乱无章,而不会让每个人都为之疯狂(或使其无法构建您的应用)。

So here’s the full guide to NPM. In order to understand this tutorial, you just need to know the difference between front-end v. backend.

因此,这里是NPM的完整指南。 为了理解本教程,您只需要知道frontend。v。backend之间的区别 。

什么是NPM? (What Is NPM?)

Imagine that you are the director of a new Hollywood movie. After accepting the role, you immediately need to begin hiring actors and other executives to create the movie alongside you.

假设您是一部好莱坞新电影的导演。 接受角色后,您立即需要开始雇用演员和其他主管人员来与您一起制作电影。

Of course, that brings the immediate question: which actors/actresses will you hire? How will you make sure that they can all work together? How will you fit it within budget?

当然,这带来了紧迫的问题:您会雇用哪些演员? 您如何确保他们都能一起工作? 您如何在预算范围内调整它?

Or, will you go in a completely different direction, and try to build the perfect team from scratch with relatively unknown actors?

或者,您会朝着完全不同的方向前进,并尝试与相对未知的演员从头开始建立一支完美的团队吗?

If you want to hire out a team, you will need to look at some sort of directory to find the right people. I don’t know if Hollywood has some sort of internal directory, but IMDB is one that comes to mind. Or, in the days, before the Internet, there was probably even a physical directory.

如果您想雇用一个团队,则需要查看某种目录以找到合适的人。 我不知道好莱坞是否有某种内部目录,但是IMDB是我想到的一个目录。 或者,在互联网问世之前,甚至可能存在一个物理目录。

Just like IMDB has information on thousands of actors that you can use to make a hiring decision, NPM has hundreds of thousands of “packages” that offer specialized functions. They are all written in JavaScript, so TECHNICALLY you could rewrite them… but that is not the point. They are meant to make your life easier by making new functionality instantly available.

就像IMDB拥有有关成千上万的参与者的信息,您可以用来做出雇用决定一样,NPM也拥有成千上万的提供专门功能的“软件包”。 它们都是用JavaScript编写的,因此从技术上讲,您可以重写它们……但这不是重点。 它们旨在通过立即提供新功能来使您的生活更轻松。

It’s just like hiring a known actor/actress — that person is a specialist in certain types of roles due to past experience.

就像聘用知名演员一样,由于过去的经验,该人是某些类型角色的专家。

We all know Liam Neeson’s specialty…

我们都知道利亚姆·尼森的专长……

So, as a developer, you access the NPM registry to add specific packages to your web app, which should make your life easier. And just like all actors/actresses have built their own careers, all NPM packages have been built by individual developers or teams and contributed to the registry.

因此,作为开发人员,您可以访问NPM注册表以将特定的程序包添加到Web应用程序,这将使您的生活更轻松。 就像所有演员/女演员都建立了自己的职业生涯一样,所有NPM软件包都是由单个开发人员或团队构建的,并为注册表做出了贡献。

Within your code, all of these packages are tracked in the package.json file. So, that file is kind of like the list of people that are involved in the movie. Here is an example of that list:

在您的代码中,所有这些包都在package.json文件中进行了跟踪。 因此,该文件有点像电影中涉及的人员列表。 这是该列表的示例:

We will cover the versions later in this tutorial.

我们将在本教程的后面部分介绍这些版本。

包装说明 (Packages Explained)

Let’s imagine that your movie has 100 roles that need to be filled. If you had an unlimited budget, would you want to fill all those roles with famous actors/actresses that had played similar roles in the past?

假设您的电影有100个角色需要填补。 如果预算没有限制,您是否想用过去扮演过类似角色的著名演员来填补所有这些角色?

Probably not.

可能不是。

Everyone would want to be the star of the show and bend the rules to fit their character. It would be a nightmare. But, if you only hire unknown actors, it is going to be pretty tough to make an excellent movie!

每个人都想成为节目的明星,并改变规则以适应他们的角色。 这将是一场噩梦。 但是,如果您只雇用不知名的演员,那么制作一部优秀的电影将非常困难!

It takes years of experience to become a great actor, after all.

毕竟,要成为一名出色的演员需要多年的经验。

Similarly, when you are building a web app, you are constantly faced with a choice — can I build this functionality myself, or should I use a package to accomplish the task?

同样,在构建Web应用程序时,您总是面临一个选择-我可以自己构建此功能,还是应该使用软件包来完成任务?

Do you know the movie “Super Troopers“? It’s a comedy classic, and the budget was just $3 million dollars. However, most excellent movies cost $10–100 million dollars. You can build an incredible web app from scratch, but you probably want to use packages.

你知道电影《 超级士兵 》吗? 这是一部喜剧经典,预算只有300万美元。 然而,大多数优秀的电影花费了10亿到1亿美元。 您可以从头开始构建一个令人难以置信的Web应用程序,但是您可能想使用软件包。

Let’s say that you are hiring Mark Wahlberg, a famous American actor, for your movie. When Mark joins your movie, it is not just him. He has a team of people that support him and make him successful. A chef, a trainer, an agent.

假设您要雇用美国著名演员Mark Wahlberg来拍摄电影。 当马克加入您的电影时,不仅是他。 他有一群人支持他并使他成功。 厨师,培训师,经纪人。

Similarly, each individual package does not operate independently. The package authors used other NPM packages to make their lives easier too. In fact, their package will share some dependencies with other packages, just like Mark Wahlberg might share a private chef with other actors and actresses.

同样,每个单独的程序包也不独立运行。 软件包作者使用其他NPM软件包也使他们的生活更轻松。 实际上,他们的包裹将与其他包裹共享一些依赖关系,就像Mark Wahlberg可能与其他演员共享女演员的私人厨师一样。

Here’s the list of dependencies for request, a popular package:

这是流行的软件包request的依赖项列表:

So let’s return to our package.json example from above:

因此,让我们从上面返回我们的package.json示例:

When you add the “axios” package to your app, you are not just adding the axios file. You are also adding any dependencies that the axios file will need, if you had not previously added them with another package. Those are not explicitly stated, but you can always find them within the node_modules folder.

当您将“ axios ”软件包添加到您的应用程序时,您不仅在添加axios文件。 如果您以前没有将axios文件添加到其他软件包中,则还需要添加它们。 这些没有明确说明,但是您始终可以在node_modules文件夹中找到它们。

This is one of the benefits of npm. When you add a new package, you don’t even need to check if you are already using all the necessary packages that support the package that you are using. npm will automatically add the new ones to your directory.

这是npm的好处之一。 添加新软件包时,甚至不需要检查是否已经在使用支持所用软件包的所有必要软件包。 npm会自动将新的添加到您的目录中。

版本说明 (Versions Explained)

Do you see the three sets of numbers next to each package above? That’s the version number. Since developers are constantly updating their packages, you can choose to use a specific version of a package, or automatically use the latest version.

您在上面的每个包装旁边都看到三组数字吗? 那是版本号 。 由于开发人员会不断更新其软件包,因此您可以选择使用特定版本的软件包,也可以自动使用最新版本。

So, when you are using 40 different packages in your project, and they are all constantly shifting, you may find that compatibility issues arise. For example, when ReactJS releases its newest version, your app may no longer function as you would expect. That’s where testing comes into play, but that’s the subject of another tutorial.

因此,当您在项目中使用40个不同的软件包并且它们都在不断变化时,您可能会发现兼容性问题。 例如,当ReactJS发行其最新版本时,您的应用程序可能不再像您期望的那样运行。 这就是测试起作用的地方,但这是另一个教程的主题。

Think of it as actors/actresses at different stages of their career. Some may play similar roles throughout their career, while others may change drastically.

可以将其视为处于职业不同阶段的演员。 有些人可能在整个职业生涯中扮演相似的角色,而另一些人可能会发生巨大变化。

Young vs. old Clint Eastwood

年轻与老克林特·伊斯特伍德

使用命令行 (Using the Command Line)

When you download Node and NPM to your local machine, you can instantly use a variety of commands to work with the NPM directory. One common one is:

将Node和NPM下载到本地计算机时,可以立即使用各种命令来处理NPM目录。 一种常见的是:

npm install

npm安装

If you want to install the express package, you would type in the command line:

如果要安装Express软件包,请在命令行中输入:

npm install express

NPM安装快递

Then, NPM would download the express code and its dependencies to your local computer. That’s kind of like the act of hiring a new actor. Or —

然后,NPM将快速代码及其依赖项下载到本地计算机。 这有点像雇用新演员的行为。 要么 -

npm uninstall express

npm卸载快递

That’s like “firing” the express package from your app.

这就像从您的应用程序“触发”快递包。

You can add new commands in the “scripts” section of your package.json file. One common one is “start”, which means “start running the node server”. It looks like this:

您可以在package.json文件的“脚本”部分中添加新命令。 一种常见的启动方式是“启动”,即“启动运行节点服务器”。 看起来像这样:

Kind of like a director yelling “Action!”

有点像导演大喊“动作!”

依赖性说明 (Dependencies Explained)

So far, we have only briefly touched on the concept of dependencies. Your app will likely have a couple packages that only run on the local version, like testing and transpiling tools. In other words, you should have a couple tools that you only use in your local environment, and not on the production version.

到目前为止,我们仅简要介绍了依赖项的概念。 您的应用可能会包含几个只能在本地版本上运行的软件包,例如测试和编译工具。 换句话说,应该有几个只能在本地环境中使用的工具,而不能在生产版本中使用。

If you need a refresher on the difference between localhost and production, check out this guide.

如果您需要对本地主机和生产之间的区别进行复习, 请查看本指南 。

Think about all the time and energy that goes into making a movie. Only a small fraction of it occurs on the official set with the cameras rolling. Behind the scenes, there are hours of work on memorizing lines, lifting weights in the gym and learning new accents. To make the team more effective, the director might hire specialized coaches for each one of these functions.

想一想电影制作所花费的所有时间和精力。 只有很少一部分出现在相机滚动的正式场景中。 在幕后,有很多工作要做,例如背线,在健身房举重和学习新的口音。 为了提高团队效率,主管可能会为这些职能中的每一项雇用专门的教练。

So, when we look at all the packages being used by a Node app, we can actually divide it up into two categories:

因此,当我们查看Node应用程序正在使用的所有软件包时,实际上可以将其分为两类:

  1. Packages used in both production and local生产和本地使用的包装
  2. Packages just used locally仅在本地使用的软件包

Grunt is a task runner that automates repetitive commands on the command line. Nodemon automatically restarts your server upon any changes in your server code.

Grunt是一个任务运行程序,可以自动执行命令行上的重复命令。 服务器代码中的任何更改时, Nodemon都会自动重新启动服务器。

In your package.json file, these are separated into two sections: dependencies and devDependencies. Here is what that looks like:

在package.json文件中,它们分为两部分:依赖关系和devDependencies。 看起来像这样:

翻译自: https://www.freecodecamp.org/news/node-package-manager-npm-explained-by-directing-a-movie-9c90f1d16d33/

npm 包管理器

npm 包管理器_导演电影解释了节点软件包管理器(NPM)相关推荐

  1. python进入上下文管理器_浅谈Python中with(上下文管理器)的用法

    例子一 首先来看一段代码: classFoo(object):def __init__(self):print('实例化一个对象')def __enter__(self):print('进入')def ...

  2. docker可视化管理界面_分析一款Docker容器可视化管理工具Porttainer

    Porttainer介绍 Portainer是一款可视化.轻量级的容器镜像的图形管理工具,利用Portainer可以轻松构建,管理和维护Docker主机及Swarm群集,提供状态显示面板.应用模板快速 ...

  3. excel切片器_听说你还不会用切片器?比筛选好用100倍,小白也能学会

    私信回复关键词[2020]~ 获取100+套高颜值图表模板! 大家好,我是还在写毕业论文的小E~ 临近毕业,还要搞 PPT!心累啊! 这不,我在看关于怎么做毕业论文 PPT 的时候,无意中看到了这样的 ...

  4. 安卓手机阅读器_罗辑思维旗下得到APP发布全新阅读器,7.8英寸安卓9.0系统

    昨天,静读君刚发布了小米多看电纸书众筹的消息:小米也要做电纸书!Kindle将迎来最强挑战者? 没想到,又有一款新的阅读器即将发布:得到阅读器 得到阅读器是由罗辑思维旗下得到APP发布的一款电纸书,与 ...

  5. 城市轨道交通运营票务管理论文_(毕业论文)轨道交通票务管理.doc

    江苏联合职业技术学院 苏州建设交通分院 毕业论文 题目 轨道交通票务管理 摘 要 地铁,轻轨以其安全.舒适.方便.快捷等突出优点成为大城市改善交通结构,构筑立体交通运输网络,解决交通拥挤难题,改善城市 ...

  6. 实验六软件包管理linux,oeasy教您玩转linux010202软件包管理apt

    原标题:oeasy教您玩转linux010202软件包管理apt 顾一下

  7. 欢乐鼠标自动点击器_使用ESP32 DIY蓝牙自动点击器,滑屏器

    前言 因为爱薅羊毛,然后正好最近免费车一趟接一趟,咪咕开完,京东开,京东开完掌阅开,下一个不知道是谁,,,,可我又不是什么很有耐心的人,虽然我喜欢看书,但是如果规定时间,加上页数限制我就很烦,所以我还 ...

  8. python管理团队_敏捷项目管理实战之团队自我管理

    什么是自我管理 自我管理就是充分发挥团队成员的自主性,通过使团队在不需要(或极少需要)专门的管理者的干预下仍然能够正常运作的方式达到提供管理效率的一种管理思想.就其具体实践而言,笔者认为主要有以下几个 ...

  9. python写小说阅读器_用python实现自己的小说阅读器!

    前一段时间书荒的时候,在喜马拉雅APP发现一个主播播讲的小说-大王饶命.听起来感觉很好笑,挺有意思的,但是只有前200张是免费的,后面就要收费.一章两毛钱,本来是想要买一下,发现说的进度比较慢而且整本 ...

最新文章

  1. Python组织文件 实践:查找大文件、 用Mb、kb显示文件尺寸 、计算程序运行时间
  2. PHP在线小说txt生成器源码
  3. solr 高并发_Solr搜索引擎 — SolrCloud介绍和环境准备
  4. Android内容提供器的应用,基于Android的智能终端应用防护系统短信过滤子模块的设计与实现...
  5. Python生成器对象send()方法用法
  6. (一二四)给类对象赋值、以及类对象的返回值
  7. linux之rpm命令
  8. Visual Studio 2015出现Cannot find one or more components. Please reinstall the application.的问题解决
  9. 【机器学习算法实现】主成分分析(PCA)——基于python+numpy
  10. Centos7 minimal 安装npm
  11. 翻译润色、论文英语修改、语法建议、3个软件-研究生必用
  12. Android 360分包,如何安装分包app安装包
  13. 微信怎么和计算机发送文件格式,微信电脑版文档怎么弄 文档发送教程
  14. android 短信列表优化,android listview优化几种写法详细介绍
  15. STP生成树协议基础
  16. android视频自动旋转,Android 使用PLDroidPlayer播放网络视频 根据视频角度自动旋转...
  17. 防劫持工具,介绍几款浏览器劫持修复工具
  18. 事件绑定-addEventListener()和attachEvent()的区别及用法
  19. 剑指Offer--约瑟夫环问题
  20. JavaScript数组空位的处理

热门文章

  1. 大连印象_2010暑期实训有感【一】
  2. 可迭代对象与迭代器 0318 草稿
  3. linux-修改pip包的下载源-改为国内境象提升速度
  4. SQL Sever sa密码丢失
  5. 用大数据为互联网金融保驾护航
  6. RxJava Agera 从源码简要分析基本调用流程(2)
  7. 修改环境变量后,导致一些常用命令失效,如ll,ls,vi不能用
  8. 来聊一聊window.fetch
  9. ModuleNotFoundError: No module named 'cv2'
  10. 如何在CentOS上设置MariaDB Galera Cluster 10.0