npm 升级软件包
Here’s a little guide for publishing your own packages on npm.
这是在npm上发布自己的软件包的一些指南。
You’ve written some piece of software code that you think is really useful! Now you want publish it on npm so that others can use your wonderful bundle of code!
您已经编写了一些您认为确实有用的软件代码! 现在,您要在npm上发布它,以便其他人可以使用您的精美代码!
There’s a lot of configurations for npm but many times these will be project-specific. This article is just going to cover the essentials for getting your packages published on npm.
npm有很多配置,但是很多时候这些都是特定于项目的。 本文仅介绍在npm上发布软件包的基本知识。
npm is included with Node.js. To check if npm is installed on your system, run this command in your terminal: npm -v
npm包含在Node.js中。 要检查系统上是否安装了npm,请在终端上运行以下命令: npm -v
Let’s create a folder that’s going to hold our package’s source code. In your terminal:
让我们创建一个文件夹,其中将包含我们程序包的源代码。 在您的终端中:
# This will create, and navigate
# into the `wonderful-bundle` directory
$ mkdir wonderful-bundle
$ cd wonderful-bundle
Now that you’re in the folder, here’s where we start using npm
commands!
现在您已经在文件夹中,这是我们开始使用npm
命令的地方!
$ npm init
Running npm init
will ask you a few setup questions (for example, the name of your package, your package’s description, etc).
运行npm init
将询问您一些设置问题(例如,软件包的名称,软件包的说明等)。
You can simply hit “Enter” for each question and this default boilerplate for package.json
file will be created in your directory:
您只需为每个问题按“ Enter”,然后package.json
文件的默认样板将在您的目录中创建:
{
"name": "wonderful-bundle",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC"
}
You can compare the package.json file to a recipe card for your favorite meal.
您可以将package.json文件与您最喜欢的一餐的食谱卡进行比较。
The package.json
file contains all of the descriptive metadata about the project (like the name “Apple Crumb Pie”), and all of the dependencies needed to properly run it (the ingredients: “apples”, “pie crust”, “sugar”, etc).
package.json
文件包含有关项目的所有描述性元数据(例如名称“ Apple Crumb Pie”),以及正确运行该项目所需的所有依赖项(成分:“苹果”,“饼皮”,“糖” ”等)。
Using all of this info, npm brings everything together so that your package can be effortlessly downloaded & run by other people.
使用所有这些信息,npm将所有内容组合在一起,以便其他人可以轻松下载和运行您的软件包。
Let’s edit package.json
to include a description, and author information.
让我们编辑package.json
以包括描述和作者信息。
{
"name": "wonderful-bundle",
"version": "1.0.0",
"description": "outputs an uplifting message",
"main": "index.js",
"author": "Chompy MacPherson ",
"license": "ISC"
}
The only fields that are required in package.json
are “name”, “version”, and “main”. The “scripts” field was removed since we don’t have any tests written yet.
package.json
中唯一需要的字段是“ name”,“ version”和“ main”。 由于我们尚未编写任何测试,因此删除了“脚本”字段。
The “main” field is the file path to the JavaScript code. When someone uses your package this JavaScript file will be used. Let’s create the index.js
file in our terminal:
“ main”字段是JavaScript代码的文件路径。 当有人使用您的软件包时,将使用此JavaScript文件。 让我们在终端中创建index.js
文件:
$ touch index.js
In a text editor of your choice…
在您选择的文本编辑器中…
module.exports = function() {
console.log("you're wonderful!");
return;
};
Remember to export
your code in the same way you would for files that are local to your software project.
请记住,以与软件项目本地文件相同的方式export
代码。
Generally it’s a good idea to include documentation for your package so others know how to use it. The README file is typically used for this purpose.
通常,最好为您的软件包包括文档,以便其他人知道如何使用它。 自述文件通常用于此目的。
Let’s create the README file in the root of your package’s directory:
让我们在包目录的根目录中创建README文件:
# create the README file
$ touch README
# put some text into README
$ echo "## Wonderful Bundle \n\n Get an uplifting message!" > README
Currently, this is what the file directory for wonderful-bundle
looks like:
当前,这是wonderful-bundle
的文件目录,如下所示:
wonderful-bundle
|_ index.js
|_ README
|_ package.json
Essentially, this is the basic structure of a npm package. Not much is needed to publish your software to npm!
本质上,这是npm软件包的基本结构。 将您的软件发布到npm不需要太多!
Now that we feel pretty good about our package, let’s publish it!
现在我们对我们的程序包感觉很好,让我们发布它!
$ npm publish
You’ll need an account on the npm registry website, and if you’re not logged into it from the CLI you’ll be asked to log in. You also have to use a package name that hasn’t been used on the registry already.
您需要在npm注册表网站上拥有一个帐户,并且如果未从CLI登录,则将要求您登录。您还必须使用未在注册表中使用的软件包名称。已经。
That’s it! Your package is now published on npm. To recap, there are only 3 steps to go from zero-to-published:
而已! 您的软件包现已发布在npm上 。 回顾一下,从零到发布只有3个步骤:
Initialize: npm init
初始化: npm init
Add source code: index.js
and README
添加源代码: index.js
和README
Publish: npm publish
发布: npm publish
Now when someone wants to use your package, they just run this in their terminal:
现在,当有人想使用您的软件包时,他们只需在终端中运行它:
$ npm install wonderful-bundle
This will download, and install any dependencies needed for your package in other people’s software projects! Technology is amazing
这将下载并在其他人的软件项目中安装软件包所需的任何依赖项! 技术很棒amazing
Hopefully this guide has shown you how easy it is to contribute your software to the Open Source community, regardless of how significant or small it might be
希望本指南向您展示了向开放源代码社区贡献软件的难易程度,无论它的重要性如何。
If you prefer using Yarn check out this guide: npm vs Yarn Commands Cheat Sheet
如果您更喜欢使用Yarn,请查看此指南: npm vs Yarn Commands作弊表
翻译自: https://www.digitalocean.com/community/tutorials/workflow-publishing-first-package-to-npm
npm 升级软件包