TypeScript NPM package template
MIT License
This is a template repository for creating a NPM package with TypeScript.
@johngeorgewright/ts-module
to your new package name@johngeorgewright
& ts-module
individuallyprivate
property from package.json
(if you want to publically publish your module)secrets.
in the .github
diectory and make sure you have the appropriate secrets registered in GitHub (Your Repo > Settings > Secrets)release.config.js
to yarn npm publish --access public
By default, this project's dependencies is kept up-to-date with renovate. This project may also be set-up for dependabot too. To do so:
renovate.json
filemv .github/.dependabot.yml .github/dependabot.yml
This template is designed to help create libraries that produce both ES and CommonJS modules. It will create both the CommonJS and ESM distribution files.
You must take in to consideration, however, that the CommonJS distribution requires the filenames to have the extension .cjs
and therefore will be converted automatically. What will not be converted automatically, however, is the import statements. For example, imagine you have the following:
// src/my-module.ts
export function myFunction() {}
// src/util.ts
export function myUtil() {}
// src/index.ts
import { myFunction } from './my-module.js'
import { myUtil } from './util.js'
The CommonJS distribution will fail in the above example as my-module.js
and util.js
will not exist. Instead they will be my-module.cjs
nd util.cjs
.
Therefore, we recommend making use of the exports
and imports
feature of the package.json.
{
"name": "my-package",
"exports": {
"./my-module": {
"import": "./dist/es/my-module.js",
"require": "./dist/common/my-module.cjs"
}
},
"imports": {
"#util": {
"import": "./dist/es/util.js",
"require": "./dist/common/util.cjs"
}
}
}
// src/index.ts
import { myFunction } from 'my-package/my-module'
import { myUtil } from '#util'