Packaging files for npm with .npmignore
dist) and there is your result. Set the
files field in your package.json-file to
dist folder, but you don't want to unnecessarily publish test files.
If you list any files or folders in your .gitignore file, they will also be excluded from your npm package. Test files is something that you definitely want to check in with git though, so putting them in .gitignore is not an option.
The files field in your
package.json takes an array of patterns that will tell npm what to include in your package. While it might be possible to come up with a clever combination of patterns that will include all your desired files and nothing more, now and in the future, it is much too complicated in my opinion, especially since there is an easier way.
You can place a separate file in your root folder, called .npmignore, that will tell npm exactly what files to ignore for packaging purposes. This will make npm disregard .gitignore though, so be aware of that. Using the scenario with typescript test files, this is what a .npmignore file could look like:
# Ignore every file in the repo
# Then unignore the dist folder
# Then re-ignore the compiled test files
# Ignore typescript build info
This provides a readable and understandable way to specify exactly what should be included in your npm package.
Use the command
npm publish --dry-run while trying this out. You will get a list of the exact content of your package-to-be which is very useful when trying out configurations like this.
Once configured, Yarn will work the same way as npm with the .npmignore file.