How do I include built files when publishing my npm package?

I need my npm package to deliver both the original code in ‘src’ and the compiled output in ‘lib’. Currently, only ‘src’ installs. How can I add ‘lib’?

{
  "packageName": "toolkit",
  "version": "1.0.0",
  "entryPoint": "lib/start.js"
}

To include your built files when publishing your package, you can modify your package.json file to explicitly list the directories to include. For example, by adding a “files” field with both “src” and “lib”, you ensure that npm packs these directories according to your specifications. This method provides precise control over the published assets and avoids accidentally including other files. I have applied this approach in a previous project where separating source and built files was essential for maintaining package clarity in production.

try checking your .npmignore file to be sure lib isnt excluded. i fixed this by simply adding ‘lib’ to the files field in package.json. makes sure your built files get published without any mishaps

In my experience managing similar setups, I found that automating the build process before publishing can help ensure that the right files are packaged. Using a prepublish script that not only builds but also verifies that the built output exists prevents overlooking files. It’s crucial to double-check the .npmignore file so that it doesn’t unintentionally exclude the lib folder. For me, integrating these steps into the package’s release cycle provided an extra layer of confidence that every necessary file lands on npm properly.

A thorough method I’ve used involves simulating your package before actual publishing using the npm pack command. This allows you to inspect the tarball and verify that both the src and lib directories are present. I encountered an issue where the .npmignore file was unintentionally excluding the lib folder. To avoid this, I explicitly defined which files to include in package.json and adjusted the ignore patterns accordingly. This proactive check ensured that all necessary assets were delivered without surprises to the end-users.

i fixed it by using a prepare script to autogen the lib folder before publish. this run-before idea keeps npm pack in sync and saved me from manual checks. sometimes npm pack can hide missing files issues, so it’s a neat prepublish trick.