package.json files字段 & .npmignore
发布于 8 年前 作者 magicdawn 10719 次浏览 来自 分享

package.json files & .npmignore

https://docs.npmjs.com/files/package.json#files

The “files” field is an array of files to include in your project. If you name a folder in the array, then it will also include the files inside that folder. (Unless they would be ignored by another rule.) You can also provide a “.npmignore” file in the root of your package or in subdirectories, which will keep files from being included, even if they would be picked up by the files array. The .npmignore file works just like a .gitignore.

  • package.json files 就是白名单, 不写这个字段就默认是所有文件
  • .npmignore 则是黑名单, 排除掉, 即使在 files 里写了, 还是会排除掉. 该文件还可以写在子文件夹中

例外

不管怎么配置 package.json / README.md / CHANGELOG.md / LICENSE / LICENCE 都会包含在 publish 的 package 中

不管怎么配置, 下面这些都不会被包含

.git
CVS
.svn
.hg
.lock-wscript
.wafpickle-N
*.swp
.DS_Store
._*
npm-debug.log

以前造的小轮子都把 package.json / README.md / CHANGELOG.md / LICENSE 写在 files 里, 今天再查发现它们会自动包含… 为宇宙的熵增着想, 我们应该忽略它, let npm take care of it…

4 回复

估计不久之后会有product模式。。。

你好。发现既然白名单就能解决问题,为啥很多项目还要黑名单一起用呢

@cuiyongjian

可能是 npm 规则经常改, 让人搞不懂…

可以用我们写的小工具自动化配置 https://github.com/popomore/ypkgfiles

回到顶部