npm软件安装包_如何发布您的第一个npm软件包

npm软件安装包

What makes JavaScript great to work with is its vast ecosystem of useful packages. There’s a package for almost any problem you might encounter.

使JavaScript很棒的原因是其庞大的有用软件包生态系统。 几乎可以解决您遇到的所有问题的软件包。

Need a date library? No problem, here’s a package just for you! Need a form validation library? The community has you covered!

需要日期库吗? 没问题,这是只为您准备的包裹! 需要表单验证库吗? 社区覆盖了您!

Behind each package is an individual or a team. Those people are working super hard to make things possible. Why not learn how things work behind the scenes by creating and publishing your very first npm package?

每个包裹的背后都是一个人或一个团队。 这些人正在努力工作,以使事情成为可能。 为什么不通过创建和发布第一个npm软件包来了解事物在幕后的工作方式?

Without further ado, let’s jump in!

事不宜迟,让我们开始吧!

安装Node.js (Installing Node.js)

Go to the official Node.js website, and press install. Node also includes the Node package manager (npm) by default. Therefore, you only need to install Node to get started with publishing npm packages.

转到Node.js官方网站,然后按install。 默认情况下,Node还包括Node软件包管理器(npm)。 因此,您只需要安装Node即可开始发布npm软件包。

Image for post
Node.js website Node.js网站

登录到npm(Logging Into npm)

Once you’ve installed Node, head over to the official npm website, and create an account. Your published packages will be under that account.

安装Node后,请转到npm官方网站并创建一个帐户。 您发布的软件包将在该帐户下。

Image for post
Signing into an npm account via the npm CLI
通过npm CLI登录npm帐户

开始一个新的节点项目(Starting a New Node Project)

Go ahead and type the following command into the console. The command will create a new folder and initialize a Node project inside that folder.

继续,在控制台中键入以下命令。 该命令将创建一个新文件夹,并在该文件夹中初始化一个Node项目。

mkdir is-even && cd is-even && npm init -y
Image for post
Creating a new Node project
创建一个新的Node项目

创建npm包(Creating the npm Package)

Next, let’s create the package itself. Create a file called index.js — this is where the underlying functionality of the package will be.

接下来,让我们创建包本身。 创建一个名为index.js的文件-这是程序包的基础功能所在。

We’re going to issue a super-simple package — a function that calculates if the input number is an even number or not. Based on the calculation, the function returns true or false.

我们将发布一个超简单的程序包—一个计算输入数字是否为偶数的函数。 根据计算,该函数返回truefalse

const isEven = (number) => {
  return number % 2 === 0
}


module.exports = isEven

更新package.json(Updating the package.json)

Every Node project has a package.json file. The package.json file does the following:

每个Node项目都有一个package.json文件。 package.json文件执行以下操作:

  • Lists the packages your project depends on

    列出您的项目所依赖的软件包
  • Specifies versions of a package that your project uses

    指定项目使用的软件包的版本
  • Makes your build reproducible and sharable with other developers

    使您的构建可与其他开发人员共享和共享
{
  "name": "is-even-[your-name-here]",
  "version": "1.0.1",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "keywords": [
    "example",
    "tutorial"
  ],
  "author": "",
  "license": "ISC"
}

The package.json file must contain the "name" and "version" fields.

package.json文件必须包含"name""version"字段。

The "name" field contains your package’s name and must be lowercase and one word. The name field may contain hyphens (-) and underscores (_).

"name"字段包含软件包的名称,并且必须为小写字母和一个单词。 name字段可以包含连字符( - )和下划线( _ )。

The "version" field must be in the form x.x.x and follow the semantic-versioning guidelines.

"version"字段的格式必须为xxx并遵循语义版本指南

If you want to include package author information in the "author" field, use the following format. The email and website are both optional.

如果要在"author"字段中包含程序包作者信息,请使用以下格式。 电子邮件和网站都是可选的。

Your Name <email@example.com> (http://example.com)

创建自述文件 (Creating a README)

The README file is meant to help other developers find your packages on npm and to hopefully aid in having a good experience while using your code. I strongly recommend including a README file in your package directory.

README文件旨在帮助其他开发人员在npm上找到您的软件包,并希望有助于在使用代码时获得良好的体验。 我强烈建议您在程序包目录中包含一个README文件。

Your README file may include directions for installing and configuring and instructions for using the code in your package. You can also include any other information a user may find helpful. The README file will be shown on the package page.

您的自述文件可能包括安装和配置说明以及使用程序包中代码的说明。 您还可以包括用户可能会有所帮助的任何其他信息。 自述文件将显示在软件包页面上。

touch readme.md

And here’s an example of what I’d include my README file for an npm package.

这是一个示例,其中包含了我的npm软件包的自述文件。

In case you're curious, here’s the raw version of the README file.

如果您感到好奇,这是README文件的原始版本

创建一个GitHub仓库 (Creating a GitHub Repository)

We’re almost ready to publish the code. Go ahead a create a GitHub repository.

我们几乎准备发布代码。 继续创建GitHub存储库。

图片发布
Creating a GitHub repository
创建一个GitHub仓库

将代码推送到GitHub存储库(Pushing the Code to the GitHub Repository)

Let’s push the code to your repository. Follow the instructions below.

让我们将代码推送到您的存储库中。 请按照以下说明进行操作。

图片发布
GitHub repository
GitHub存储库

And here’s what you should end up with:

这就是您应该得到的结果:

图片发布
is-even GitHub repository is-even GitHub存储库

发布npm软件包(Publishing the npm Package)

We’re finally ready to publish the npm package. We can publish our package with a single command.

我们终于准备好发布npm软件包了。 我们可以用一个命令发布我们的包。

 npm publish
图片发布

Voila! And that’s i — see how easy it was to publish a package to the npm registry?

瞧! 那就是我-看到将软件包发布到npm注册表有多么容易?

图片发布
is-even package 均等包装

测试包装(Testing the Package)

Testing and using the package we published earlier is straightforward. We can either install it with npm or Yarn.

测试和使用我们之前发布的软件包非常简单。 我们可以使用npm或Yarn安装它。

Go ahead and start the new Node project, and install the package.

继续并启动新的Node项目,然后安装该软件包。

图片发布
Installing the package we published earlier to our fresh project
将我们先前发布的软件包安装到我们的新项目中

Require the package, and call the function like so. Remember, the function we exported was called isEven, but you can rename it to whatever you want.

需要包,并像这样调用函数。 请记住,我们导出的函数称为isEven ,但是您可以将其重命名为所需的任何名称。

图片发布
Importing our newly published package in a fresh project
在一个新项目中导入我们新发布的软件包

Well done! You did it — you published your first npm package.

做得好! 完成了–您发布了第一个npm软件包。

奖励:np (Bonus: np)

Manually bumping the version, running tests, and publishing the package can get tedious — especially if you have more than a couple of packages open-sourced already. That’s why I’d recommend using the np package.

手动修改版本,运行测试以及发布程序包可能会很乏味-特别是如果您已经拥有多个开源程序包。 这就是为什么我建议使用np包的原因。

图片发布
np p

安装 (Install)

np can be configured both locally and globally.

可以在本地和全局配置np

 $ npm install --global np

互动式使用者介面 (Interactive UI)

Run np without arguments to launch the interactive UI that guides you through publishing a new version.

运行不带参数的np以启动交互式UI,该UI指导您发布新版本。

图片发布

And here’s what tasks it’s going to perform for you:

以下是它将为您执行的任务:

图片发布

结论 (Conclusion)

Thanks for reading, and I hope you learned something new! Happy coding.

感谢您的阅读,希望您能学到新知识! 快乐的编码。

翻译自: https://medium.com/better-programming/how-to-publish-your-first-npm-package-ebfbcde3c26f

npm软件安装包

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值