The pubspec file
Every pub package needs some metadata so it can specify its dependencies. Pub packages that are shared with others also need to provide some other information so users can discover them. All of this metadata goes in the package’s pubspec: a file named pubspec.yaml
that’s written in theYAML language.
Supported fields
A pubspec can have the following fields:
name
- Required for every package. Learn more.
version
- Required for packages that are hosted on the Pub site. Learn more.
description
- Required for packages that are hosted on the Pub site. Learn more.
author
orauthors
- Optional. Learn more.
homepage
- Optional. URL pointing to the package’s homepage (or source code repository). Learn more.
repository
- Optional. URL pointing to the package’s source code repository. Learn more.
issue_tracker
- Optional. URL pointing to an issue tracker for the package. Learn more.
documentation
- Optional. URL pointing to documentation for the package. Learn more.
dependencies
- Can be omitted if your package has no dependencies. Learn more.
dev_dependencies
- Can be omitted if your package has no dev dependencies. Learn more.
dependency_overrides
- Can be omitted if you do not need to override any dependencies. Learn more.
environment
- Required as of Dart 2. Learn more.
executables
- Optional. Used to put a package’s executables on your PATH. Learn more.
publish_to
- Optional. Specify where to publish a package. Learn more.
Pub ignores all other fields,
If you add a custom field, give it a unique name that won’t clash with future pubspec fields. For example, instead of adding bugs
, you might add a field named my_pkg_bugs
.
Example
A simple but complete pubspec looks something like the following:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
<span class="kwd">name: </span><span class="pln">newtify </span><span class="kwd">version: </span><span class="pln">1.2.3 </span><span class="kwd">description: </span><span class="pun">>-</span><span class="pln"> Have you been turned into a newt</span><span class="pun">?</span><span class="pln"> Would you like to be</span><span class="pun">?</span><span class="pln"> This package can help. It has all of the newt</span><span class="pun">-</span><span class="pln">transmogrification functionality you have been looking for. </span><span class="kwd">author: </span><span class="pln">Natalie Weizenbaum <nweiz@google.com</span><span class="pun">></span> <span class="kwd">homepage: </span><span class="pln">https</span><span class="pun">:</span><span class="pln">//example</span><span class="pun">-</span><span class="pln">pet</span><span class="pun">-</span><span class="pln">store.com/newtify </span><span class="kwd">documentation: </span><span class="pln">https</span><span class="pun">:</span><span class="pln">//example</span><span class="pun">-</span><span class="pln">pet</span><span class="pun">-</span><span class="pln">store.com/newtify/docs </span><span class="kwd">environment: </span> <span class="kwd">sdk: </span><span class="str">'>=2.0.0 <3.0.0'</span> <span class="kwd">dependencies: </span> <span class="kwd">efts: </span><span class="pln">^2.0.4 </span><span class="kwd">transmogrify: </span><span class="pln">^0.4.0 </span><span class="kwd">dev_dependencies: </span> <span class="kwd">test: </span><span class="str">'>=0.6.0 <0.12.0'</span> |
Details
This section has more information about most of the pubspec fields.
Name
Every package needs a name. It’s how other packages refer to yours, and how it appears to the world, should you publish it.
The name should be all lowercase, with underscores to separate words, just_like_this
. Use only basic Latin letters and Arabic digits: [a-z0-9_]
. Also, make sure the name is a valid Dart identifier—that it doesn’t start with digits and isn’t a reserved word.
Try to pick a name that is clear, terse, and not already in use. A quick search of packages on the Pub site to make sure that nothing else is using your name is recommended.
Version
Every package has a version. A version number is required to host your package on the Pub site, but can be omitted for local-only packages. If you omit it, your package is implicitly versioned 0.0.0
.
Versioning is necessary for reusing code while letting it evolve quickly. A version number is three numbers separated by dots, like 0.2.43
. It can also optionally have a build ( +1
, +2
, +hotfix.oopsie
) or pre-release (-dev.4
, -alpha.12
, -beta.7
, -rc.5
) suffix.
Each time you publish your package, you publish it at a specific version. Once that’s been done, consider it hermetically sealed: you can’t touch it anymore. To make more changes, you’ll need a new version.
When you select a version, follow semantic versioning.
Description
This is optional for your own personal packages, but if you intend to publish your package you must provide a description, which should be in English. The description should be relatively short—60 to 180 characters—and tell a casual reader what they might want to know about your package.
Think of the description as the sales pitch for your package. Users see it when they browse for packages. The description is plain text: no markdown or HTML.
Author/authors
You’re encouraged to use these fields to describe the author(s) of your package and provide contact information. Use author
if your package has a single author, or use authors
with a YAML list if more than one person wrote the package. Each author can be either a single name (Natalie Weizenbaum
) or a name and an email address (Natalie Weizenbaum <nweiz@google.com>
). For example:
1 2 3 |
<span class="kwd">authors: </span><span class="pun">-</span><span class="pln"> Natalie Weizenbaum <nweiz@google.com</span><span class="pun">></span> <span class="pun">-</span><span class="pln"> Bob Nystrom <rnystrom@google.com</span><span class="pun">></span> |
The email addresses can be from any provider. If anyone uploads your package to the Pub site, then the author information (including email addresses) becomes public.
Homepage
This should be a URL pointing to the website for your package. For hosted packages, this URL is linked from the package’s page. While providing a homepage
is optional, please provide it or repository
(or both). It helps users understand where your package is coming from.
Repository
The optional repository
field should contain the URL for your package’s source code repository — for example, https://github.com/<user>/<repository>
. If you publish your package to the Pub site, then your package’s page displays the repository URL. While providing a repository
is optional, please provide it or homepage
(or both). It helps users understand where your package is coming from.
Issue tracker
The optional issue_tracker
field should contain a URL for the package’s issue tracker, where existing bugs can be viewed and new bugs can be filed. The Pub site attempts to display a link to each package’s issue tracker, using the value of this field. If issue_tracker
is missing butrepository
is present and points to GitHub, then the Pub site uses the default issue tracker (https://github.com/<user>/<repository>/issues
).
Documentation
Some packages have a site that hosts documentation, separate from the main homepage and from the Pub-generated API reference. If your package has additional documentation, add a documentation:
field with that URL; pub shows a link to this documentation on your package’s page.
Dependencies
Dependencies are the pubspec’s raison d’être. In this section you list each package that your package needs in order to work.
Dependencies fall into one of two types. Regular dependencies are listed under dependencies:
—these are packages that anyone using your package will also need. Dependencies that are only needed in the development phase of the package itself are listed under dev_dependencies
.
During the development process, you might need to temporarily override a dependency. You can do so using dependency_overrides
.
For more information, see Package dependencies.
Executables
A package may expose one or more of its scripts as executables that can be run directly from the command line. To make a script publicly available, list it under the executables
field. Entries are listed as key/value pairs:
1 |
<name-of-executable>: <Dart-script-from-bin> |
For example, the following pubspec entry lists two scripts:
1 2 3 |
<span class="kwd">executables: </span> <span class="kwd">polymer-new-element: </span><span class="pln">new_element useful</span><span class="pun">-</span><span class="pln">script</span><span class="pun">:</span> |
Once the package is activated using pub global activate
, typing polymer-new-element
executes bin/new_element.dart
. Typing useful-script
executes bin/useful-script.dart
. If you don’t specify the value, it is inferred from the key.
For more information, see pub global.
Publish_to
The default uses the Pub site. Specify none
to prevent a package from being published. This setting can be used to specify a custom pub package server to publish.
1 |
<span class="kwd">publish_to: </span><span class="pln">none</span> |
SDK constraints
A package can indicate which versions of its dependencies it supports, but packages have another implicit dependency: the Dart platform itself. The Dart platform evolves over time, and a package might only work with certain versions of the platform.
A package can specify those versions using an SDK constraint. This constraint goes inside a separate top-level environment
field in the pubspec and uses the same version constraint syntax as dependencies.
For example, the following constraint says that this package works with any Dart 2 SDK that’s version 2.0.0 or higher:
1 2 |
<span class="kwd">environment: </span> <span class="kwd">sdk: </span><span class="str">'>=2.0.0 <3.0.0'</span> |
Pub tries to find the latest version of a package whose SDK constraint works with the version of the Dart SDK that you have installed.
Flutter SDK constraints
As of Dart 1.19.0, pub supports Flutter SDK constraints under the environment:
field:
1 2 3 |
<span class="kwd">environment: </span> <span class="kwd">sdk: </span><span class="str">'>=1.19.0 <3.0.0'</span> <span class="kwd">flutter: </span><span class="pln">^0.1.2</span> |
A Flutter SDK constraint is satisfied only if pub is running in the context of the flutter
executable, and the Flutter SDK’s version
file matches the given version constraint. Otherwise, the package will not be selected.
To publish a package with a Flutter SDK constraint, you must specify a Dart SDK constraint with a minimum version of at least 1.19.0, to ensure that older versions of pub won’t accidentally install packages that need Flutter.