选择命令行版本:
🌐 Description
因此,你决定使用 npm 来开发(并且可能发布/部署)你的项目。
🌐 So, you've decided to use npm to develop (and maybe publish/deploy) your project.
极好的!
🌐 Fantastic!
在用户安装程序的简单步骤之外,你需要做一些事情。
🌐 There are a few things that you need to do above the simple steps that your users will do to install your program.
🌐 About These Documents
这些是手册页。如果你安装了 npm,你应该能够使用 man npm-thing 来获取某个特定主题的文档,或者使用 npm help thing 来查看相同的信息。
🌐 These are man pages. If you install npm, you should be able to then do man npm-thing to get the documentation on a particular topic, or npm help thing to see the same information.
🌐 What is a Package
一个包是:
🌐 A package is:
<name>@<version> 与 (c)<name>@<tag><name>git URL,当克隆时,会得到 (a)。即使你从不发布你的包,如果你只是想写一个 node 程序(a),你仍然可以获得使用 npm 的很多好处,也许你还想在打包后能够轻松地安装它成一个 tarball (b)。
🌐 Even if you never publish your package, you can still get a lot of benefits of using npm if you just want to write a node program (a), and perhaps if you also want to be able to easily install it elsewhere after packing it up into a tarball (b).
Git url 可以是以下形式:
🌐 Git urls can be of the form:
git://github.com/user/project.git#commit-ishgit+ssh://user@hostname:project.git#commit-ishgit+http://user@hostname/project/blah.git#commit-ishgit+https://user@hostname/project/blah.git#commit-ish
commit-ish 可以是任何标签、sha 或分支,可作为参数传递给 git checkout。默认值是仓库使用的默认分支。
🌐 The commit-ish can be any tag, sha, or branch which can be supplied as an argument to git checkout. The default is whatever the repository uses as its default branch.
🌐 The package.json File
你需要在项目根目录下有一个 package.json 文件才能用 npm 做大部分操作。这基本上就是整个接口了。
🌐 You need to have a package.json file in the root of your project to do much of anything with npm. That is basically the whole interface.
关于该文件包含内容的详细信息,请参见 package.json。至少,你需要:
🌐 See package.json for details about what goes in that file. At the very least, you need:
名称:这应该是一个用于标识你的项目的字符串。请不要使用名称来指定它是在 Node 上运行,或使用 JavaScript 编写的。你可以使用“engines”字段明确说明你的程序所需的 Node 版本(或其他),并且通常可以假设它是 JavaScript。
它不一定需要与你的 github 存储库名称匹配。
所以,node-foo 和 bar-js 是不好的名字。foo 或 bar 会更好。
版本:一个与语义化版本兼容的版本。
引擎:指定你的程序运行所需的 Node(或其他)的版本。Node 的 API 经常变化,可能会有你依赖的 bug 或新功能。务必明确说明。
作者:功劳归你。
脚本:如果你有特殊的编译或安装脚本,那么你应该将其放在 scripts 对象中。你至少应该在“scripts.test”字段中有一个基本的冒烟测试命令。参见 scripts。
main:如果你有一个作为程序入口点的单一模块(就像“foo”包在 require("foo") 时提供给你的那样),那么你需要在“main”字段中指定它。
目录:这是一个将名称映射到文件夹的对象。最适合包含的是“lib”和“doc”,但是如果你使用“man”来指定一个包含手册页的文件夹,它们也会像这些一样被安装。
你可以在你的包根目录使用 npm init 来帮你快速生成一个比较基础的 package.json 文件。更多信息请参见 npm init。
🌐 You can use npm init in the root of your package in order to get you started with a pretty basic package.json file. See npm init for more info.
🌐 Keeping files out of your Package
使用 .npmignore 文件将内容排除在包之外。如果没有 .npmignore 文件,但有 .gitignore 文件,那么 npm 会忽略被 .gitignore 文件匹配的内容。如果你想包含被 .gitignore 文件排除的内容,可以创建一个空的 .npmignore 文件来覆盖它。像 git 一样,npm 会在包的所有子目录中查找 .npmignore 和 .gitignore 文件,而不仅仅是根目录。
🌐 Use a .npmignore file to keep stuff out of your package. If there's no .npmignore file, but there is a .gitignore file, then npm will ignore the stuff matched by the .gitignore file. If you want to include something that is excluded by your .gitignore file, you can create an empty .npmignore file to override it. Like git, npm looks for .npmignore and .gitignore files in all subdirectories of your package, not only the root directory.
.npmignore 文件遵循与 .gitignore 文件相同的 模式规则:
# 开头的行会被忽略。/ 来指定一个目录。! 来取反该模式。默认情况下,会忽略以下路径和文件,因此无需将它们显式添加到 .npmignore 中:
🌐 By default, the following paths and files are ignored, so there's no need to add them to .npmignore explicitly:
.*.swp._*.DS_Store.git.gitignore.hg.npmignore.npmrc.lock-wscript.svn.wafpickle-*config.gypiCVSnpm-debug.log此外,node_modules 中的所有内容都会被忽略,除了打包的依赖。npm 会自动为你处理,所以无需将 node_modules 添加到 .npmignore。
🌐 Additionally, everything in node_modules is ignored, except for bundled dependencies. npm automatically handles this for you, so don't bother adding node_modules to .npmignore.
以下路径和文件永远不会被忽略,因此将它们添加到 .npmignore 是没有意义的:
🌐 The following paths and files are never ignored, so adding them to .npmignore is pointless:
package.jsonREADME(及其变体)CHANGELOG(及其变体)LICENSE / LICENCE如果考虑到你项目的结构,发现 .npmignore 很难维护,你可以尝试填写 package.json 的 files 属性,它是一个应包含在你的包中的文件或目录名称数组。有时手动选择允许的项比构建一个阻止列表更容易管理。
🌐 If, given the structure of your project, you find .npmignore to be a maintenance headache, you might instead try populating the files property of package.json, which is an array of file or directory names that should be included in your package. Sometimes manually picking which items to allow is easier to manage than building a block list.
.npmignore 或 files 配置是否有效🌐 Testing whether your .npmignore or files config works
如果你想再次确认你的包在发布时只包含你打算包含的文件,你可以在本地运行 npm pack 命令,它会在工作目录中生成一个 tar 包,就像发布时一样。
🌐 If you want to double check that your package will include only the files you intend it to when published, you can run the npm pack command locally which will generate a tarball in the working directory, the same way it does for publishing.
🌐 Link Packages
npm link 旨在安装开发包,并实时查看更改,而无需反复重新安装。当然,你仍然需要重新链接或使用 npm rebuild -g 来更新已编译的包。
更多信息请参见 npm link。
🌐 More info at npm link.
🌐 Before Publishing: Make Sure Your Package Installs and Works
这是重要的。
如果你无法在本地安装它,那么在尝试发布时会遇到问题。更糟的是,你可能能够发布它,但你发布的将是一个损坏的或毫无意义的包。所以不要那样做。
🌐 If you can not install it locally, you'll have problems trying to publish it. Or, worse yet, you'll be able to publish it, but you'll be publishing a broken or pointless package. So don't do that.
在包的根目录中,执行以下操作:
🌐 In the root of your package, do this:
npm install . -g
这会让你看到它正在工作。如果你更愿意只创建一个指向你的工作目录的符号链接包,那么可以这样做:
🌐 That'll show you that it's working. If you'd rather just create a symlink package that points to your working directory, then do this:
npm link
使用 npm ls -g 查看它是否存在。
🌐 Use npm ls -g to see if it's there.
要测试本地安装,请进入其他文件夹,然后执行以下操作:
🌐 To test a local install, go into some other folder, and then do:
cd ../some-other-foldernpm install ../my-package
将其本地安装到其他地方的 node_modules 文件夹中。
🌐 to install it locally into the node_modules folder in that other place.
然后进入 node-repl,尝试使用 require("my-thing") 来引入你模块的主模块。
🌐 Then go into the node-repl, and try using require("my-thing") to bring in your module's main module.
🌐 Create a User Account
使用 adduser 命令创建用户。操作如下:
🌐 Create a user with the adduser command. It works like this:
npm adduser
然后按照提示进行操作。
🌐 and then follow the prompts.
这在 npm adduser 中有更好的文档说明。
🌐 This is documented better in npm adduser.
🌐 Publish your Package
这部分很简单。在你的文件夹根目录下,执行以下操作:
🌐 This part's easy. In the root of your folder, do this:
npm publish
你可以给发布一个 tarball 的 url,或者一个 tarball 的文件名,或者一个文件夹的路径。
🌐 You can give publish a url to a tarball, or a filename of a tarball, or a path to a folder.
请注意,几乎该文件夹中的所有内容默认都会被公开。所以,如果里面有机密内容,请使用 .npmignore 文件列出要忽略的通配符,或者从新的检发布本发布。
🌐 Note that pretty much everything in that folder will be exposed by default. So, if you have secret stuff in there, use a .npmignore file to list out the globs to ignore, or publish from a fresh checkout.
🌐 Brag about it
在 IRC 中发送电子邮件、写博客、闲聊。
🌐 Send emails, write blogs, blab in IRC.
告诉世界安装你的程序是多么容易!
🌐 Tell the world how easy it is to install your program!
🌐 See also