Algobook
- The developer's handbook
mode-switch
back-button
Buy Me A Coffee
Wed May 03 2023

How to make NPM not publish src/ and other private files to your library

Lately, we have been publishing a lot of new stuff to npm. And I thought of one point that I noticed I was doing wrong at the beginning, that I thought I could share how to fix. And that is how we can prevent certain files and folders to be uploaded to npm that we don't want. For example, if you are building your code with e.g babel or similar to production ready code, you probably don't want your not-transpiled source code to be published as well. Or if you have config files etc that you are using for development purposes for example, and the list goes on.

Solution

I will cut right to the chase. In the root of your public library, create a file called .npmignore. And in this file, we simply specify what we want npm to ignore. Example:

.babelrc myDevScript.js src/

In this example, we are telling NPM that we do not want .babelrc, myDevScript.js and our entire src folder to be uploaded. And the next time you run

npm publish

the above files not be uploaded.

Outro

Today, we shared how to make npm ignore files that we don't want to publish along with our open source project to npmjs.com I hope this guide helped you, and thanks for reading!

All the best,

signatureWed May 03 2023
See all our articles