npm pack: A Dry-Run for npm Package Publishing

May 20, 2017

Heads up! npm pack now has a --dry-run option, which will print the output of npm pack to your terminal and not generate a tarball. This wasn't available when this article was originally published. 🙌

Have you ever wondered, "Where do my files go when I publish a package to npm?" I have. I recently learned about npm pack. According to the npm publish documentation:

For a "dry run" that does everything except actually publishing to the registry, see npm pack, which figures out the files to be included and packs them into a tarball to be uploaded to the registry.

From the root of your repository, run npm pack to generate a tarball. You can run tar -ztf <name>-<version>.tgz on Unix systems to view the files in the resulting tarball. Here's an example from a Node module of mine, danger-plugin-jira-issue.

❯ tar -ztf danger-plugin-jira-issue-0.0.0-development.tgz

This is useful for ensuring that you are uploading the necessary files to be consumed by users of your Node module, or that you aren't shipping unnecessary files like tests, documentation, or dot files.

If you want to remove some of those unneeded files, add the "files" field to your package.json to control exactly what you'll ship. An example from danger-plugin-jira-issue's package.json:

  "files": ["dist", "types/index.d.ts"]

Happy publishing! 🚀