How to create and publish an npm package with TypeScript

· Category: TypeScript

Short answer

Initialize a project with package.json, configure tsconfig.json for declaration output, build with tsc, and publish using npm publish. Include type declarations so consumers get autocomplete. For module management patterns, see python pip install packages. For testing before release, see python testing with pytest.

Steps

  1. Initialize: npm init -y
  2. Install TypeScript: npm install -D typescript
  3. In tsconfig.json, set "declaration": true and "outDir": "dist"
  4. Set main and types in package.json
  5. Build and publish: npm run build && npm publish

Tips

  • Use .npmignore or files array to limit published contents
  • Test your package locally with npm link
  • For virtual environment isolation, see python virtual environments