Company Logo

Bisht Bytes

Creating npm package for typescript based library in 2024

Published On: 13 Jul 2024
Reading Time: 1 minutes

Overview


To create typescript based packages that are compatible with both ECMAScript Modules (ESM) and CommonJS (CJS) you can use the following configuration:

{
  "name": "my-npm-package",
  "version": "1.0.0",
  "description": "",
  "main": "./dist/index.js",
  "module": "./dist/index.mjs",
  "types": "./dist/index.d.ts",
  "exports": {
    ".": {
      "require": "./dist/index.js",
      "import": "./dist/index.mjs",
      "types": "./dist/index.d.ts"
    }
  },
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "build": "tsup src/index.ts --format cjs,esm --dts --clean",
    "watch": "npm run build -- --watch src",
    "prepublishOnly": "npm run build"
  },
  "keywords": [],
  "author": "",
  "license": "Apache-2.0"
}

Reference


Page Views: -