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](https://img-blog.csdnimg.cn/img_convert/68728d98e749d6b358e57442f8473247.png)
登录到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](https://img-blog.csdnimg.cn/img_convert/96019f7aea2f00ca7c7536c16d1cb086.png)
开始一个新的节点项目(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](https://img-blog.csdnimg.cn/img_convert/7aa67242c607c09b3d962501550bac55.png)
创建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
.
我们将发布一个超简单的程序包—一个计算输入数字是否为偶数的函数。 根据计算,该函数返回true
或false
。
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存储库。
![图片发布](https://img-blog.csdnimg.cn/img_convert/73df3e1cfad1d12d0eedc3a010e5da5f.png)
将代码推送到GitHub存储库(Pushing the Code to the GitHub Repository)
Let’s push the code to your repository. Follow the instructions below.
让我们将代码推送到您的存储库中。 请按照以下说明进行操作。
![图片发布](https://img-blog.csdnimg.cn/img_convert/6433141f19ce00df57b08b1d1d2c6b31.png)
And here’s what you should end up with:
这就是您应该得到的结果:
![图片发布](https://img-blog.csdnimg.cn/img_convert/9bd4bdf35aae756f8702f6386936bcf5.png)
发布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
![图片发布](https://img-blog.csdnimg.cn/img_convert/1bd6df8a37531b8c94d15c0ce130003c.png)
Voila! And that’s i — see how easy it was to publish a package to the npm registry?
瞧! 那就是我-看到将软件包发布到npm注册表有多么容易?
![图片发布](https://img-blog.csdnimg.cn/img_convert/37b804013378be7fa6e00bdc86b9e03c.png)
测试包装(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项目,然后安装该软件包。
![图片发布](https://img-blog.csdnimg.cn/img_convert/1870100680a21c2e6ceae1566b4f8d79.png)
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
,但是您可以将其重命名为所需的任何名称。
![图片发布](https://img-blog.csdnimg.cn/img_convert/f82834c397399e372967aeed4dfc8b6c.png)
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
包的原因。
![图片发布](https://img-blog.csdnimg.cn/img_convert/71690ae85dab594899d8b47bb54fd117.png)
安装 (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指导您发布新版本。
![图片发布](https://img-blog.csdnimg.cn/img_convert/be96e91c38b74573794e0ffb86c2d849.png)
And here’s what tasks it’s going to perform for you:
以下是它将为您执行的任务:
![图片发布](https://img-blog.csdnimg.cn/img_convert/4a8e174ab52a410d371a8d24c4692331.png)
结论 (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软件安装包