How to create a simple library using typescript
Github https://github.com/sil-co/my-first-library 1. Initialize the Project mkdir my-ts-lib cd my-ts-lib npm init -y 2. Install Development Dependencies npm install --save-dev typescript tsup eslint prettier 3. Create tsconfig.json npx tsc --init Then edit it like: { "compilerOptions": { "target": "ESNext", "module": "ESNext", "declaration": true, "outDir": "dist", "strict": true, "esModuleInterop": true, "skipLibCheck": true, "moduleResolution": "Node", "resolveJsonModule": true }, "include": ["src"] } 4. Create Library Code Create a src/ folder: src/ ├── index.ts Example src/index.ts: export function greet(name: string): string { return `Hello, ${name}!`; } 5. Bundle with tsup Add tsup.config.ts: import { defineConfig } from 'tsup'; export default defineConfig({ entry: ['src/index.ts'], format: ['esm', 'cjs'], dts: true, clean: true, }); Add to package.json: "scripts": { "build": "tsup" } Run: npm run build 6. Prepare for Publishing Edit package.json: { "name": "your-lib-name", "version": "1.0.0", "main": "./dist/index.js", "module": "./dist/index.mjs", "types": "./dist/index.d.ts", "files": ["dist"], "exports": { "import": "./dist/index.mjs", "require": "./dist/index.js" } } 7. Publish to npm Log in to npm: npm login Publish: npm publish --access public If the name is taken, either pick a unique name or use a scoped package. 8. Install & Use the Library Anywhere else: npm install your-lib-name Then in code: import { greet } from 'your-lib-name'; console.log(greet('World'));

Github
https://github.com/sil-co/my-first-library
1. Initialize the Project
mkdir my-ts-lib
cd my-ts-lib
npm init -y
2. Install Development Dependencies
npm install --save-dev typescript tsup eslint prettier
3. Create tsconfig.json
npx tsc --init
Then edit it like:
{
"compilerOptions": {
"target": "ESNext",
"module": "ESNext",
"declaration": true,
"outDir": "dist",
"strict": true,
"esModuleInterop": true,
"skipLibCheck": true,
"moduleResolution": "Node",
"resolveJsonModule": true
},
"include": ["src"]
}
4. Create Library Code
Create a src/
folder:
src/
├── index.ts
Example src/index.ts
:
export function greet(name: string): string {
return `Hello, ${name}!`;
}
5. Bundle with tsup
Add tsup.config.ts
:
import { defineConfig } from 'tsup';
export default defineConfig({
entry: ['src/index.ts'],
format: ['esm', 'cjs'],
dts: true,
clean: true,
});
Add to package.json
:
"scripts": {
"build": "tsup"
}
Run:
npm run build
6. Prepare for Publishing
Edit package.json
:
{
"name": "your-lib-name",
"version": "1.0.0",
"main": "./dist/index.js",
"module": "./dist/index.mjs",
"types": "./dist/index.d.ts",
"files": ["dist"],
"exports": {
"import": "./dist/index.mjs",
"require": "./dist/index.js"
}
}
7. Publish to npm
- Log in to npm:
npm login
- Publish:
npm publish --access public
If the name is taken, either pick a unique name or use a scoped package.
8. Install & Use the Library
Anywhere else:
npm install your-lib-name
Then in code:
import { greet } from 'your-lib-name';
console.log(greet('World'));