A modern template for creating and publishing NPM packages using TypeScript, TSup, and Changesets.
- 📦 TypeScript configuration with strict settings
- 🛠️ TSup for bundling (outputs CommonJS and ESM)
- 🔄 Automated versioning and publishing with Changesets
- 🚀 GitHub Actions for CI/CD
- ✨ Type definitions included
- 📝 Automated changelog generation
- Click the "Use this template" button at the top of this repository
- Clone your new repository
- Install dependencies:
npm install
- Make your changes in the
src
directory - Build the package:
npm run build
- Lint your code:
npm run lint
When you want to make changes to the package:
- Create a new branch
- Make your changes
- Add a changeset:
npm run changeset
- Commit and push your changes
- Create a Pull Request
.
├── .changeset/
├── .github/
│ └── workflows/
│ ├── main.yaml
│ └── publish.yaml
├── src/
│ └── index.ts
├── .gitignore
├── package.json
├── tsconfig.json
└── README.md
npm run build
- Builds the package using TSupnpm run lint
- Runs TypeScript type checkingnpm run changeset
- Creates a new changesetnpm run version
- Applies changesets to create a new versionnpm run release
- Publishes the package to NPM
name: CI
on:
push:
branches:
- "**"
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Setup Node.js
uses: actions/setup-node@v3
with:
node-version: 20.x
cache: "npm"
- name: Install Dependencies
run: npm install --frozen-lockfile
- name: Lint and Build
run: npm run lint && npm run build
name: Publish
on:
workflow_run:
workflows: [CI]
branches: [main]
types: [completed]
concurrency: ${{ github.workflow }}-${{ github.ref }}
permissions:
contents: write
pull-requests: write
jobs:
publish:
if: ${{ github.event.workflow_run.conclusion == 'success' }}
runs-on: ubuntu-latest
permissions:
contents: write
pull-requests: write
steps:
- uses: actions/checkout@v3
- name: Setup Node.js
uses: actions/setup-node@v3
with:
node-version: 20.x
cache: "npm"
- name: Install Dependencies
run: npm install --frozen-lockfile
- name: Create Release Pull Request or Publish
id: changesets
uses: changesets/action@v1
with:
publish: npm run release
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
NPM_TOKEN: ${{ secrets.NPM_TOKEN }}
{
"name": "pkg-template",
"repository": {
"type": "git",
"url": "git+https://github.com/codebygio/pkg-template.git"
},
"version": "0.0.1",
"description": "A template for creating npm packages",
"main": "dist/index.js",
"module": "dist/index.mjs",
"types": "dist/index.d.ts",
"scripts": {
"build": "tsup src/index.ts --format cjs,esm --dts",
"release": "npm run build && changeset publish",
"lint": "tsc"
},
"keywords": [
"npm",
"package",
"template"
],
"author": "Giovani Rodriguez",
"license": "MIT",
"devDependencies": {
"@changesets/cli": "^2.27.9",
"tsup": "^8.3.5",
"typescript": "^5.6.3"
}
}
{
"compilerOptions": {
/* Base Options: */
"esModuleInterop": true,
"skipLibCheck": true,
"target": "es2022",
"verbatimModuleSyntax": true,
"allowJs": true,
"resolveJsonModule": true,
"moduleDetection": "force",
/* Strictness */
"strict": true,
"noUncheckedIndexedAccess": true,
/* If NOT transpiling with TypeScript: */
"moduleResolution": "Bundler",
"module": "ESNext",
"noEmit": true,
/* If your code runs in the DOM: */
"lib": [
"es2022",
"dom",
"dom.iterable"
],
}
}
node_modules/
dist/
.DS_Store
- Make sure you have an NPM account and are logged in:
npm login
-
Add an NPM_TOKEN secret to your GitHub repository:
- Generate a token on NPM's website
- Add it to your repository's secrets as
NPM_TOKEN
-
When you're ready to publish:
- Merge your changes to main
- The GitHub Action will automatically create a release PR
- Merge the release PR to publish to NPM
MIT
- Fork the repository
- Create your feature branch
- Add a changeset describing your changes
- Commit your changes
- Push to the branch
- Open a Pull Request
Don't forget to customize this README with your package's specific details!