con un clic
npm-library-setup
// Comprehensive guidance on setting up npm libraries with package.json, with a preference for ES Modules (ESM). Use when setting up npm packages, configuring ESM, TypeScript packages, or React component libraries.
// Comprehensive guidance on setting up npm libraries with package.json, with a preference for ES Modules (ESM). Use when setting up npm packages, configuring ESM, TypeScript packages, or React component libraries.
Use only when the user explicitly asks to stage, commit, push, and open a GitHub pull request in one flow using the GitHub CLI (`gh`).
This skill should be used when the user asks to "optimize TypeScript performance", "speed up tsc compilation", "configure tsconfig.json", "fix type errors", "improve async patterns", or encounters TS errors (TS2322, TS2339, "is not assignable to"). Also triggers on .ts, .tsx, .d.ts file work involving type definitions, module organization, or memory management. Does NOT cover TypeScript basics, framework-specific patterns, or testing.
| name | npm-library-setup |
| description | Comprehensive guidance on setting up npm libraries with package.json, with a preference for ES Modules (ESM). Use when setting up npm packages, configuring ESM, TypeScript packages, or React component libraries. |
This skill provides comprehensive guidance on setting up an npm library with package.json, with a preference for ES Modules (ESM).
This skill helps you create npm packages that:
"type": "module"exports field (no deprecated module field)Use when:
Categories covered:
Initialize your package:
npm init -y
Configure for ESM by adding "type": "module" to package.json
Install build and test tools:
npm install -D bunchee vitest
Create your source files in src/ and run npm run build
{
"type": "module",
"main": "./dist/index.js",
"types": "./dist/index.d.ts",
"exports": {
".": {
"types": "./dist/index.d.ts",
"default": "./dist/index.js"
}
},
"scripts": {
"build": "bunchee",
"test": "vitest",
"test:run": "vitest run"
},
"engines": {
"node": ">=20"
}
}
Note: Use the oldest currently-maintained LTS version (check Node.js Release Schedule).
"type": "module" for pure ESM packagesexports field instead of deprecated module field.js extensions in imports (even in TypeScript)Install TypeScript and configure:
npm install -D typescript @types/node
Create tsconfig.json:
{
"compilerOptions": {
"target": "ES2022",
"module": "ESNext",
"moduleResolution": "NodeNext",
"declaration": true,
"strict": true
}
}
Bunchee automatically compiles TypeScript and generates .d.ts files.
Install React as dev dependency:
npm install -D react @types/react
Configure peerDependencies:
{
"peerDependencies": {
"react": "*"
}
}
exports field (no deprecated module field).js)// Named exports
export function greet(name) {
return "Hello, " + name + "!";
}
// Default export
export default class MyLibrary {}
// Import
import { greet } from "./module.js";
import MyLibrary from "./MyLibrary.js";
Important: Always use .js extension in imports, even in TypeScript files.
my-package/
├── package.json
├── src/
│ ├── index.js # or index.ts
│ └── helpers.js
├── dist/ # Build output
└── README.md
See references/ directory for detailed guides:
See examples/ directory for complete working examples: