. npmignore,. Gitignore and package.json Files field in

Time:2020-8-1

This paper refers to this article https://docs.npmjs.com/misc/developers ;
Problem scenario: NPM publish publishes an NPM package. When publishing, you only want to publish the packaged files, but you do not want to publish the package source code and unit test files;
Files in. Npmignore will not be published. By default, NPM publish publishes all files in the directory except

  • .*.swp
  • ._*
  • .DS_Store
  • .git
  • .hg
  • .npmrc
  • .lock-wscript
  • .svn
  • .wafpickle-*
  • config.gypi
  • CVS
  • npm-debug.log

Therefore, it is not necessary to add these files to. Npmignore. If there is no.npmignore and there is. Gitignore, then the files in. Gitignore will be ignored from the package. If they exist at the same time, the priority of. Npmignore is better
If you want to include something that is excluded by your .gitignore file, you can create an empty .npmignore file to override it. Like git

In addition, the files in. Gitignore and. Npmignore are searched recursively. Here’s another experience:
npm looks for .npmignore and .gitignore files in all subdirectories of your package, not only the root directory.

These are the files released by default. Adding. Gitignore and. Npmignore will not take effect

  • package.json
  • README(and its variants)
  • CHANGELOG(and its variants)
  • LICENSE/LICENCE

So when you NPM publish, why is there always package.json Documents, do you understand? These files are part of the package by default when NPM publish

Introduction package.json The files in this field will not be added to the packages published by NPM publish by default. Its priority is higher than. Npmignore and. Gitignore. This is the most widely used method. It seems that many open source projects use the files field