目录
创建和发布范围公共包
目录
要在用户或组织命名空间中公开共享你的代码,你可以将公共用户范围或组织范围的包发布到 npm 注册表。
¥To share your code publicly in a user or organization namespace, you can publish public user-scoped or organization-scoped packages to the npm registry.
有关范围的更多信息,请参阅“关于范围”。
¥For more information on scopes, see "About scopes".
注意:在发布用户范围的 npm 包之前,你必须 注册 为 npm 用户账户。
¥Note: Before you can publish user-scoped npm packages, you must sign up for an npm user account.
此外,要发布组织范围的包,你必须先 创建一个 npm 用户账户,然后再 创建一个 npm 组织。
¥Additionally, to publish organization-scoped packages, you must create an npm user account, then create an npm organization.
创建范围公共包
¥Creating a scoped public package
-
如果你使用 npmrc 去 管理多个注册表上的账户,请在命令行上切换到相应的配置文件:
¥If you are using npmrc to manage accounts on multiple registries, on the command line, switch to the appropriate profile:
npmrc <profile-name>
-
在命令行上,为你的包创建一个目录:
¥On the command line, create a directory for your package:
mkdir my-test-package
-
导航到包的根目录:
¥Navigate to the root directory of your package:
cd my-test-package
-
如果你使用 git 管理你的包代码,请在包根目录中运行以下命令,将
git-remote-url
替换为你的包的 git 远程 URL:¥If you are using git to manage your package code, in the package root directory, run the following commands, replacing
git-remote-url
with the git remote URL for your package:git initgit remote add origin git://git-remote-url -
在包根目录中,运行
npm init
命令并将范围传递给scope
标志:¥In the package root directory, run the
npm init
command and pass the scope to thescope
flag:-
对于组织范围的包,将
my-org
替换为你的组织名称:¥For an organization-scoped package, replace
my-org
with the name of your organization:npm init --scope=@my-org
-
对于用户范围的包,将
my-username
替换为你的用户名:¥For a user-scoped package, replace
my-username
with your username:npm init --scope=@my-username
-
-
根据提示生成
package.json
文件。如需命名包的帮助,请参阅“包名指南”。¥Respond to the prompts to generate a
package.json
file. For help naming your package, see "Package name guidelines". -
创建一个 README 文件,说明你的包代码是什么以及如何使用它。
¥Create a README file that explains what your package code is and how to use it.
-
在你首选的文本编辑器中,为你的包编写代码。
¥In your preferred text editor, write the code for your package.