Skip to content

Tags

On this page

Nodejs Monorepo

Tools

GitHub - atlassian/changesets: 🦋 A way to manage your versioning and changelogs with a focus on monorepos

Use Changesets as monorepo manager

Init a monorepo

mkdir monorepo && cd monorepo
git init
# Add .gitignore file for nodejs <https://github.com/github/gitignore/blob/master/Node.gitignore>
npm init --yes

Add "private":"true" to the root package.json

npm init -w packages/a
npm init -w packages/b
npm init -w packages/c

Let c depends a and b,

Add

"dependencies": {
"a":"^1.0.0",
"b":"^1.0.0"
}

to packages/c/package.json

Install changesets

Also see here

npm install -D @changesets/cli && npx changeset init

This action will add a .changeset folder, and a couple of files to help you out:

You should change the .changeset/config.json -> baseBranch to yourself main branch, also change access to public

Commit current files.

git add .
git commit -m "feat: init"

That's done.

Usage

First publish

First publish you should just use the follow command to publish your module

npx changeset publish

Future changes

You should see changesets philosophy

You should first create an intent to change, that said, before what ever changes you want to make, you should create a intent change

npx changeset

...Make some changes

Now, generate new version, changeset will take care your dependences,

Note, by default, if a upgrade from 1.0.1 to 1.0.2, c depends on [email protected]^1.0.1, then c package.json will not change, cause npm will auto update [email protected]^1.0.1 to the last version 1.0.2 if you want change to the exact version every time, you can set the config to

"___experimentalUnsafeOptions_WILL_CHANGE_IN_PATCH": {
"updateInternalDependents": "always"
}
npx changeset version

Then, you can publish it.

npx changeset publish
git add .
git commit -m "chore: version"
git push --follow-tags

With CI

I'll use Github Actions as example.

The CI workflow assume that you use Git workflows as your git workflow.

Note, you can use @changesets/changelog-github as your changelog format log. with this, you can generate a log like this , without this, then the log will only include commit link > npm i -D @changesets/changelog-github

{
"changelog": [
"@changesets/changelog-github",
{ "repo": "theowenyoung/monorepo-example" }
]
}
  1. Create a script in root package.json
{
"scripts": {
"version-packages": "changeset version",
"release": "changeset publish"
}
}
  1. Create github actions workflows
mkdir -p .github/workflows
touch .github/workflows/release.yml

With the following content:

name: Release
on:
workflow_dispatch:
push:
branches:
- main
jobs:
release:
name: Release
runs-on: ubuntu-latest
steps:
- name: Checkout Repo
uses: actions/[email protected]
with:
# This makes Actions fetch all Git history so that Changesets can generate changelogs with the correct commits
fetch-depth: 0
- name: Setup Node.js 12.x
uses: actions/setup-[email protected]
with:
node-version: 12.x
- name: Setup NPM Latest
run: npm i -g npm
- name: Install Dependencies
run: npm i
- name: Create Release Pull Request or Publish to npm
uses: changesets/[email protected]
with:
# this expects you to have a script called release which does a build for your packages and calls changeset publish
publish: npm run release
version: npm run version-packages
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
NPM_TOKEN: ${{ secrets.NPM_TOKEN }}
  1. Add NPM_TOKEN to your github repo secret settings

By Nodejs Tips:

npm token create

Done.

Every time you use npx changeset to generate a new changeset intent, and the change is pulled to the main branch, then CI will create a pull request to generate a new version, and after the pull request was merged, CI will publish npm packages, and create a new release.

Edit this page
Last updated on 8/29/2021