DEV Community

scl
scl

Posted on • Updated on

Publishing my Typescript package to npm

(Managed to get one working here: https://www.npmjs.com/package/api-gateway-direct-dynamodb)

Documenting the steps to publish a Typescript project to npm.

Check that tsconfig.json is emitting declaration files:

    "declaration": true, 
    "outDir": "./dist",
Enter fullscreen mode Exit fullscreen mode

Compiles all Typescript source files and generates type declarations files:

rm -rf ./dist
npx tsc
Enter fullscreen mode Exit fullscreen mode

Check that ./dist contains the compiled .js and .d.ts files.

Edit package.json to inform users' compilers on where to find the type declarations:

  "main": "dist/index.js",
  "types": "dist/index.d.ts",
Enter fullscreen mode Exit fullscreen mode

This next step is optional, which is to test the library locally before publishing. Create another empty project and try to installing the library:

npm install path/to/my-package
Enter fullscreen mode Exit fullscreen mode

Finally, publish the library:

npm publish
Enter fullscreen mode Exit fullscreen mode

(For subsequent republishing, we would also need to increment the version number in package.json.

Ref: https://medium.com/cameron-nokes/the-30-second-guide-to-publishing-a-typescript-package-to-npm-89d93ff7bccd

Top comments (0)