typescript-npm-package-template 1.0.7
Install from the command line:
Learn more about npm packages
$ npm install @warstekhun/typescript-npm-package-template@1.0.7
Install via package.json:
"@warstekhun/typescript-npm-package-template": "1.0.7"
About this version
Based on Tom Chen's amazing template
- Building for severeal enviroments:
- ESM
- CommonJS
- UMD
- Your to be published package'll can be ran both in browsers and with NodeJS
- Tested on:
- NodeJS: 16.x | 17.x | 18.x
- Webpack: 5
- (Qwik: 1.26)
- Tested on:
- Creates d.ts by default
- Supports tests with coverage with JEST
- Outputs coverage badges automatically on build
- Github Actions tests
- After cloning the repo simply run
npm install
to install the required dependencies. - Configurate package.json for your needs
- Write your code
- Publish your package!
- All the tests are located in the ./test folder.
- You can run them with:
-
npm run test
for simple testing -
npm run test:cov
for coverage testing. Will also open the coverage test results in your default browser.
-
- You can run them with:
- Format youre code with Prettier:
-
npm run format:fix
: Automatically fixes formatting. -
npm run format:check
: "Does not fix, only checks if fixing is needed, outputs results"
-
- Build your project with:
-
npm run build
: Builds the whole project -
npm run build:esm
: Builds only the ESM version -
npm run build:cjs
: Builds only the CommonJS version -
npm run build:umd
: Builds only the UMD version -
npm run build:types
: Builds only the types (d.ts)
-
- Incrementing package version, creating commit:
-
npm run publish
or simplynpm run publish
:- Builds the project
- Increments version automatically.
- Asks for new package version or level of incrementing
- Just like using npm version. Default: patch
- Asks for custom commit message. Default: incremented version number
- Asks for new package version or level of incrementing
- Creates new commit and tag
- Pushes to repository
-
Run:
npm link
npm link will create a symlink in the global folder, which may be {prefix}/lib/node_modules/example-typescript-package or C:\Users<username>\AppData\Roaming\npm\node_modules\example-typescript-package.
Create an empty folder elsewhere, you don't even need to npm init
(to generate package.json because your linked package won't show up in package.json). Open the folder with VS Code, open a terminal and just run:
npm link typescript-npm-package-template
This will create a symbolic link from globally-installed example-typescript-package to node_modules/ of the current folder. Then you'll be able to import your project by using its name property's value from package.json
import { Num } from 'typescript-npm-package-template';
console.log(new Num(5).add(new Num(6)).val() === 11)
Log in:
npm adduser
And publish:
npm publish
- Publish your package to NPM and Github Packages at the same time!
Follow npm's official instruction to create an npm token. Choose "Publish" from the website, or use npm token create
without argument with the CLI.
If you use 2FA, then make sure it's enabled for authorization only instead of authorization and publishing (Edit Profile -> Modify 2FA).
On the page of your newly created or existing GitHub repo, click Settings -> Secrets -> New repository secret, the Name should be `NPM_TOKEN`` and the Value should be your npm token.
The default configuration of this example package assumes you publish package with an unscoped name to npm. GitHub Packages must be named with a scope name such as "@tomchen/example-typescript-package".
Change scope: '@warstekhun'
to your own scope in .github/workflows/publish.yml, also change addscope
in package.json.
If you want to publish your package with a scoped name, change the name property in package.json and the scope from @warstekhun to yours at .github/workflows/publish.yml:47.
If you publish your package to npm only, and don't want to publish to GitHub Packages, then delete the lines from - name: Setup .npmrc file to publish to GitHub Packages
to the end of the file in .github/workflows/publish.yml.
(You might have noticed secret.GITHUB_TOKEN
in .github/workflows/publish.yml. You don't need to set up a secret named GITHUB_TOKEN
actually, it is automatically created)
- Creating and publishing unscoped public packages - npm docs
- npm-publish - npm docs
- Publishing - TypeScript docs
- Publishing Node.js packages - GitHub Docs
Also, because this template project is a remastered version of Tom Scott's, if you want to publish a Python package, you should definetly start your project with his Example PyPI (Python Package Index) Package & Tutorial / Instruction / Workflow for 2021 template.