重要通知: 此文档针对的是 Yarn 1 (Classic) 版本。
对于 Yarn 2+ 文档及迁移指南,请访问 yarnpkg.cn。

发布一个包

In order to share your package with other developers around the world through Yarn, you’ll first need to publish it.

When you publish a package with Yarn it goes onto the npm registry which is used to distribute packages globally.

Logging into npm

If you haven’t already, you’ll first need to create an npm account. Once you’ve done that you can setup your username and email in Yarn.

yarn login

This will prompt you for your username and email. However, it will not ask you for your password. Yarn does not hold onto your password or any sessions. When you go to publish or modify something on npm, you will need to enter your password then.

Publishing your package

Once you have written all the code in your package, tested it out and you are ready to publish you can kick things off:

yarn publish

First you will be asked to enter a new version to publish:

[1/4] Bumping version...
info Current version: 1.0.0
question New version: _____

Next you will be asked to enter your npm password:

[2/4] Logging in...
info npm username: your-npm-username
info npm username: you@example.com
question npm password: ____________

Finally, Yarn will publish the package and revoke your session token.

[3/4] Publishing...
success Published.
[4/4] Revoking token...
success Revoked login token.
✨  Done in 10.53s.

Each time you want to publish a new version of your package you can follow this same flow.

Accessing your package

Your package should now be available at https://www.npmjs.com/package/my-new-project and you should be able to install it:

yarn add my-new-project

You can also see all the info in the npm registry:

yarn info my-new-project
{ name: 'my-new-project',
  description: 'My New Project description.',
  'dist-tags': { latest: '1.0.0' },
  versions: [ '1.0.0' ],
  maintainers: [ { name: 'Your Name', email: 'you@example.com' } ],
  time:
  { modified: '2023-03-30T14:55:36+00:00',
    created: '2023-03-30T14:55:36+00:00',
    '1.0.0': '2023-03-30T14:55:36+00:00' },
  homepage: 'https://my-new-project-website.com/',
  keywords: [ 'cool', 'useful', 'stuff' ],
  repository:
   { url: 'https://example.com/your-username/my-new-project',
     type: 'git' },
  contributors:
   [ { name: 'Your Friend',
       email: 'their-email@example.com',
       url: 'http://their-website.com' },
     { name: 'Another Friend',
       email: 'another-email@example.com',
       url: 'https://another-website.org' } ],
  author: { name: 'Your Name', email: 'you@example.com' },
  bugs: { url: 'https://github.com/you/my-new-project/issues' },
  license: 'MIT',
  readmeFilename: 'README.md',
  version: '1.0.0',
  main: 'index.js',
  files: [ 'index.js', 'lib/*.js', 'bin/*.js' ],
  bin: { 'my-new-project-cli': 'bin/my-new-project-cli.js' },
  dist:
   { shasum: '908bc9a06fa4421e96ceda243c1ee1789b0dc763',
     tarball: 'https://registry.npmjs.org/my-new-project/-/my-new-project-1.0.0.tgz' },
  directories: {} }