npm tips and tricks

Image of Author
September 26, 2022 (last updated May 15, 2023)

A good resource for discovering popular libraries is npm trends.

Building libraries

Bundle your code

If you are building a npm library, consider using a tool like tsup to bundle your source code into a simple dist/ directory. Bundler tools will let you write typescript while still compiling to .js, .cjs, and create .d.ts files as well.

Use package.files

Use package.files to keep your library size small. For example, say you have a "dist" folder of your bundled javascript project. To only include that, you can defined the following.

  "files": ["dist"]

Certain files are always included, so you can be minimalistic. To see what your downloadable tarball would look like, you can run npm publish --dry-run and examine the output.