Important Note
This guide covers setting up a publishable package from scratch. A quick start for publishable packages is available through @rootreeweb/quick-start
Prerequisites
-
The package name is scoped correctly (starts with @rootreeweb/**) and is unique.
- Note that you must re-run
yarn installafter changing the package name inpackage.jsonto ensure that the package is installed correctly.
- Note that you must re-run
-
You have authenticated with the registry. To do this, run
npm login --scope=@rootreeweb --auth-type=legacy --registry=https://npm.pkg.github.comProvide it with a Personal Access Token (Classic) that has access to write:packages. See Using Our Private Packages for more details on access tokens if necessary.
Initial Publishing Configuration
-
Specify the repository’s publishing configuration in
package.jsonwith:{ "publishConfig": { "{Your user name}:registry": "https://npm.pkg.github.com" } }Otherwise, the package will be published to the public NPM registry, and not the GitHub registry that we use for private packages.
-
Ensure that
"private": trueis NOT present in the
package.jsonfile. This will prevent the package from being published to any registry. -
Ensure that the
mainfield inpackage.jsonis set to the correct entry point for the package. This is typicallydist/index.jsfor JavaScript packages."main": "dist/index.js" -
Ensure that the
filesfield inpackage.jsonis set to the correct files to be included in the package. This is typicallydist/for JavaScript packages."files": [ "dist/" ] -
Ensure that the
scriptsfield inpackage.jsonincludes abuildscript that will build the package. This is typicallytscfor TypeScript packages:"scripts": { ...other scripts... "build": "tsc" } -
In the
scriptsfield, add a ‘release’ script that will run the build script and then publish the package. This is typically the following:"scripts": { ...other scripts... "release": "yarn build && npm publish" }(Note: do not name the script
publish. This will cause the package to attempt to publish twice each time it is run.) -
Ensure that the
versionfield inpackage.jsonexists and is set to the correct version number. This is typically0.0.0for new packages."version": "0.0.0"
Publishing (Or Updating an Existing Package)
-
If someone else set up the existing configuration, ensure that the publishConfig in
package.jsonincludes your GitHub username. (Step 1 of the Initial Publishing Configuration). -
Update the version number in
package.jsonto the new version number. The version number should follow Semantic Versioning. The registry will not allow you to publish a package with the same version number as an existing package. For example, if the current version is1.0.0, the next version could be1.0.1for a patch,1.1.0for a minor, or2.0.0for a major. -
Run the release script to build and publish the package:
yarn release -
If the package is new, or if the version number was updated, the package will be published to the registry. If the version number was not updated, the package will not be published, and you will receive an error message.
-
Make sure to commit and push any changes that were made. Publishing will not update the repository, only the package.
Updating a Package in a Project
-
Either
yarn add @rootreeweb/{package-name}@{version}Where
{package-name}is the name of the package and{version}is the new version number to install.Or alternatively:
yarn upgrade-interactiveTo interactively upgrade any out-of-date packages in the project. This is typically the quicker and easier option. (Note that versions of Yarn before 4.0.0 require extra steps that are not covered here.)