by @huozhi
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)