npm 升级软件包
It’s simple to publish a package onto npm. There are two steps:
将软件包发布到npm很简单。 分两个步骤:
- Create your package. 创建您的包。
- Publish the package. 发布程序包。
But publishing packages the way the industry does it? Not so simple. There are more steps. We’ll go through what steps are required, and I’ll show you an easy way to publish and update your package.
但是以行业的方式发布软件包吗? 没那么简单。 还有更多步骤。 我们将逐步介绍所需的步骤,并向您展示一种发布和更新软件包的简便方法。
创建第一个包 (Creating your first package)
This section is for you if you haven’t published a package to npm before. Feel free to skip to the next section if you published one before.
如果您之前尚未将软件包发布到npm,则此部分适用于您。 如果您之前曾发表过,请随时跳到下一部分。
To publish your first package to npm, you need to go through these steps:
要将您的第一个软件包发布到npm,您需要执行以下步骤:
First, you need to have an npm account. Create one here if you don’t have one yet.
首先,您需要有一个npm account 。 如果还没有,请在这里创建一个。
Second, you need to login to your npm account through the command line. (You need to have Node and npm installed on your system before you perform this step. Install them here).
其次,您需要通过命令行登录到您的npm帐户 。 (执行此步骤之前,您需要在系统上安装Node和npm。 在此处安装它们)。
To sign in, you use npm login
.
要登录,请使用npm login
。
npm login
You’ll be prompted to enter your username, password, and email address.
系统将提示您输入用户名,密码和电子邮件地址。
Third, you need to create a package. To do so, create a folder somewhere on your computer and navigate to it. The command line version is:
第三,您需要创建一个package 。 为此,请在计算机上的某个位置创建一个文件夹,然后导航到该文件夹。 命令行版本为:
# Creating a folder named how-to-publish-to-npm mkdir how-to-publish-to-npm # Navigating into the folder cd how-to-publish-to-npm
Next, you want to begin the project with the npm init
command.
接下来,您要使用npm init
命令开始项目。
npm init
This command runs you through a few questions and creates a package.json
file for you at the end. This package.json
file contains the bare necessities you need to publish your project. (Feel free to skip questions that don’t make sense).
该命令将为您解答几个问题,并在最后为您创建一个package.json
文件。 这个package.json
文件包含发布项目所需的所有必需品。 (随意跳过没有意义的问题)。
The final step is to publish your package with the npm publish
command.
最后一步是使用npm publish
命令发布您的软件包 。
npm publish
If the package already exists on npm (because your package has the same name as another package on npm), you won’t be able to publish it. You’ll get an error.
如果该软件包已在npm上存在(因为该软件包与npm上的另一个软件包具有相同的名称),则将无法发布该软件包。 您会得到一个错误。
You’ll need to change your package name.
您需要更改包裹名称。
To change your package name, you change the name
property in the package.json
file. Here, I changed it to publishing-to-npm
.
要更改包名称,请更改package.json
文件中的name
属性。 在这里,我将其更改publishing-to-npm
。
(You can check for naming collisions by doing a search on npm, or through the npm search
command).
(您可以通过在npm search
或通过npm search
命令来检查命名冲突)。
It’s also a good idea to update the folder name as well for consistency. Here’s the command line equivalent.
同时更新文件夹名称也是一个好主意。 这是等效的命令行。
# Command to change folder names by moving everything mv how-to-publish-to-npm publishing-to-npm
Try the publish
command again. You should get a success message now.
再次尝试publish
命令。 您现在应该会收到一条成功消息。
如果您想出的每个名字都已经被拿走了怎么办 (What to do if every name you came up with is taken already)
This is a common problem since many people create packages on npm. It’s difficult to get the package name you desire sometimes. (It’s like how I can never find a good .com
domain).
这是一个常见的问题,因为许多人在npm上创建软件包。 有时很难获得您想要的软件包名称。 (就像我永远找不到好的.com
域一样)。
To combat this problem, npm lets you publish to a scope. This means you can publish the package under your own username (or npm organization), so you’re free from naming problems.
为了解决这个问题,npm允许您发布到作用域。 这意味着您可以使用自己的用户名(或npm组织)发布该程序包,因此无需担心命名问题。
To publish to a scope, you can either:
要发布到范围,您可以:
Change the
name
to@username/package-name
manually inpackage.json
在
package.json
手动将name
更改为@username/package-name
Run
npm init --scope=username
instead ofnpm init
运行
npm init --scope=username
而不是npm init
If your repository has a scope, you need to adjust the publish command slightly:
如果您的存储库有作用域,则需要稍微调整publish命令:
npm publish --access public
That’s all you need to do to publish a package to npm.
这就是将软件包发布到npm所需要做的全部工作。
Now, let’s move on to how the industry publishes packages.
现在,让我们继续探讨行业如何发布软件包。
Consider a popular framework like React. If you dig around React, you’ll notice a few things:
考虑一个流行的框架,如React。 如果您深入研究React,您会注意到以下几点:
First, React has a Github repository.
首先,React有一个Github仓库 。
Second, React is published on npm.
Third, React follows Semantic versioning (Semver for short).
第三,React遵循语义版本控制 (简称Semver)。
Fourth, each update to React has a git tag associated with it. This git tag follows Semver as well.
第四,对React的每次更新都有一个与之关联的git标签。 这个git标签也跟随Semver。
Fifth, there are release notes for every React update.
第五,每个React更新都有发行说明 。
This means publishing a package involves many steps. At the very least, you need to:
这意味着发布程序包涉及许多步骤。 至少,您需要:
- Run tests (if there are any) 运行测试(如果有)
Update
version
inpackage.json
according to Semver根据Semver更新
package.json
version
- Create a git tag according to Semver 根据Semver创建一个git标签
- Push the package to Github 将包裹推到Github
- Push the package to npm 推包裹到npm
- Create release notes for every update 为每个更新创建发行说明
It’s common to forget one of these things when we’re ready to push. Sometimes we npm publish
and we enjoy a break. When we’re back, we screw ourselves for forgetting.
当我们准备推动时,通常会忘记其中之一。 有时我们在npm publish
,我们很享受休息。 当我们回来时,我们会忘记自己。
There’s an easier way. It’s with a tool called np
.
有一种更简单的方法。 它带有一个称为np
的工具。
p (np)
np (created by Sindre Sorhus) makes it easier for us to publish packages without missing any of the steps I detailed above.
np (由Sindre Sorhus创建)使我们可以更轻松地发布软件包,而不会遗漏我上面详述的任何步骤。
安装NP (Installing np)
To install np
, you can run the following command:
要安装np
,可以运行以下命令:
npm install np
This works. But I prefer installing np
globally on my computer so I can run the np
command anywhere.
这可行。 但是我更喜欢在计算机上全局安装np
,因此我可以在任何地方运行np
命令。
sudo npm install --global np
使用np之前 (Before using np)
Before you use np
you need to make sure:
在使用np
之前,需要确保:
- Your project is a Git repository 您的项目是一个Git存储库
- It needs to have a remote 它需要一个遥控器
- You must have pushed to the remote at least once. 您必须至少已推送到遥控器一次。
- You also need to make sure your working directory is clean. 您还需要确保您的工作目录是干净的。
# Initialize Git git init # Adds a remote repository git remote add origin some-url # Commit changes git add . git commit -m "Initial Commit"
If your project is not a Git repository, you’ll get this error:
如果您的项目不是Git存储库,则会出现以下错误:
If your project doesn’t have remote, you’ll get this error (at a later part of the checks).
如果您的项目没有远程控制,则将收到此错误(在检查的稍后部分)。
If your working directory is dirty, you’ll get this error:
如果您的工作目录不干净,则会出现以下错误:
If you haven’t pushed to the Git remote at least once, np
will just hang and do nothing.
如果您至少没有一次将其推送到Git遥控器, np
只会挂起而无所事事。
使用npm (Using npm)
To use np
, you run the np
command.
要使用np
,请运行np
命令。
np
np
will prompt you to enter a Semver number.
np
将提示您输入一个Semver号。
Choose a number and np
will ask you to confirm your choice.
选择一个号码, np
会要求您确认选择。
np
then does the rest of the publishing stuff for you.
然后, np
将为您完成其余的发布工作。
运行测试时出错 (Error with running tests)
np
runs the npm test
command as part of its checks.
np
运行npm test
命令作为其检查的一部分。
If you followed the tutorial up to this point, you would get an error that looks like this:
如果您一直按照本教程进行操作,则将出现如下错误:
This happens because our npm test
command results in an error. You can try it yourself:
发生这种情况是因为我们的npm test
命令导致错误。 您可以自己尝试:
npm test
To fix this error, we need to change the test
script in package.json
file.
要解决此错误,我们需要在package.json
文件中更改test
脚本。
Right now it looks like this:
现在看起来像这样:
"scripts": { "test": "echo \"Error: no test specified\" && exit 1"},
Change it to this:
更改为此:
"scripts": { "test": "echo \"No test specified\""},
This change works because exit 1
creates an error.
此更改有效,因为exit 1
会产生错误。
With this change, np
should complete the publishing process. (Remember to commit the change before running np
).
通过此更改, np
应该完成发布过程。 (请记住在运行np
之前提交更改)。
At the end of the process, np
launches a browser window for you to write your release notes.
在此过程结束时, np
将启动一个浏览器窗口,供您编写发行说明。
In short, np
makes publishing packages much simpler!
简而言之, np
使发布程序包变得更加简单!
Thanks for reading. Did this article help you out? If it did, I hope you consider sharing it. You might help someone else out. Thanks so much!
谢谢阅读。 这篇文章对您有帮助吗? 如果确实如此,希望您考虑共享它 。 您可能会帮助别人。 非常感谢!
This article was originally posted at my blog.Sign up for my newsletter if you want more articles to help you become a better frontend developer.
本文最初发布在我的博客上。如果您想要更多的文章来帮助您成为更好的前端开发人员,请注册我的新闻通讯 。
npm 升级软件包