DEV Community

Discussion on: Reducing npm package size by 83%

Collapse
 
aminnairi profile image
Amin

Note that you can also use the files property in your package.json file to set a list of files that will be packaged along with the mandatory files. The type of this property is an array of strings so it's pretty easy to add packaged files. See this property as a whitelist. More informations on the official documentation.

Also get used to the npm publish --dry-run command. It can be useful to see what files are packaged before publishing. It can act as a test for your files property. Still in the official documentation.

Collapse
 
nombrekeff profile image
Keff

Yup, I read something about the files property yesterday, I will try this approach, as it might be cleaner!

npm publish --dry-run
Damn, thanks, I was looking for this command, and could not find it :) I published 3 beta packages to test this out '

Do you know when should we use files and when to use .npmingore?

Collapse
 
aminnairi profile image
Amin

.npmignore act as a blacklist and files act as a whitelist. I guess i'ts a matter of preferences but I would rather be lazy and only define the files that I want to publish, and ignore all the others I didn't mention by using the `files property intsead of blacklisting all others files that should not be published to NPM. Again, a matter of preferences IMO.

Thread Thread
 
nombrekeff profile image
Keff

Ok, thanks! Yup I guess it's a matter of preferences.

I would also rather define what is needed! I may change this then :)

Collapse
 
patarapolw profile image
Pacharapol Withayasakpunt

For TypeScript users, files is definitely better than empty .npmignore that found on some tutorials.

Thread Thread
 
nombrekeff profile image
Keff

Is there a reason to put it under files? In typescript I mean

better than empty .npmignore that found on some tutorials.

xD Why would they put an empty .npmignore? I guess just to keep the tutorial simple, and not add more complexity to it... they would be better of just by not having a .npmignore at all

Thread Thread
 
helderberto profile image
Helder Burato Berto

Has an article called "For the love of god, don’t use .npmignore" by Jeff Dickey with awesome content explaining why you could use "files" in package.json instead of .npmignore.

Thread Thread
 
nombrekeff profile image
Keff

Ohh, thanks, I will check it out :)

Thread Thread
 
nombrekeff profile image
Keff

I've checked the post out, there are definitely some risks about using .npmignore, like unexpectedly upload credentials or critical information, but the risk is tolerable to the project I was optimizing, I will be taking this into account in the future though!