目录
目录
作为 npm 用户,你可以创建无范围的包以在你自己的项目中使用,并将它们发布到 npm 公共注册表以供其他人在他们的项目中使用。无范围的包始终是公共的,并且仅由包名称引用:
¥As an npm user, you can create unscoped packages to use in your own projects and publish them to the npm public registry for others to use in theirs. Unscoped packages are always public and are referred to by the package name only:
package-name
有关包范围、访问权限和可见性的更多信息,请参阅“包的范围、访问级别和可见性”。
¥For more information on package scope, access, and visibility, see "Package scope, access level, and visibility".
注意:在你可以发布公共无范围的 npm 包之前,你必须 注册 为 npm 用户账户。
¥Note: Before you can publish public unscoped npm packages, you must sign up for an npm user account.
创建无范围的公共包
¥Creating an unscoped public package
-
在命令行上,为你的包创建一个目录:
¥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
命令。¥In the package root directory, run the
npm init
command. -
根据提示生成
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.
查看包内容是否有敏感或不必要的信息
¥Reviewing package contents for sensitive or unnecessary information
将敏感信息发布到注册表可能会损害你的用户,损害你的开发基础架构,修复成本高昂,并使你面临法律诉讼的风险。我们强烈建议你在将包发布到注册表之前删除敏感信息,例如私钥、密码、个人身份信息 (PII) 和信用卡数据。
¥Publishing sensitive information to the registry can harm your users, compromise your development infrastructure, be expensive to fix, and put you at risk of legal action. We strongly recommend removing sensitive information, such as private keys, passwords, personally identifiable information (PII), and credit card data before publishing your package to the registry.
对于不太敏感的信息,例如测试数据,使用 .npmignore
或 .gitignore
文件来防止发布到注册表。有关详细信息,请参阅 此文章。
¥For less sensitive information, such as testing data, use a .npmignore
or .gitignore
file to prevent publishing to the registry. For more information, see this article.