node.js NPM 是否忽略 .gitignore 中列出的文件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24942161/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 17:42:08  来源:igfitidea点击:

Does NPM ignore files listed in .gitignore?

node.jsgitnpm

提问by Ionic? Biz?u

I have a file that is generated by npm installcommand (using preinstalltask). I don't want to add it in the git repository, nor in the NPM project.

我有一个由npm install命令(使用preinstall任务)生成的文件。我不想将它添加到 git 存储库中,也不想添加到 NPM 项目中。

Supposing the file name is foo.json, I added it in .gitignorefile as foo.json.

假设文件名是foo.json,我将它添加到.gitignore文件中作为foo.json.

Is this enough to avoid uploading it on NPM registry?

这足以避免将其上传到 NPM 注册表吗?

I know I can add .npmignorefile that will surely ignore the file, but I won't add it if .gitignorealready does this.

我知道我可以添加.npmignore肯定会忽略该文件的文件,但如果.gitignore已经这样做了,我不会添加它。

回答by cmbuckley

If a project has both an .npmignoreand .gitignorefile, npm will onlyuse the .npmignorefile.

如果项目同时具有 an.npmignore.gitignorefile,npm 将使用该.npmignore文件。

From the documentation:

文档

Use a .npmignorefile to keep stuff out of your package. If there's no .npmignorefile, but there isa .gitignorefile, then npm will ignore the stuff matched by the .gitignorefile. If you wantto include something that is excluded by your .gitignorefile, you can create an empty .npmignorefile to override it.

使用.npmignore文件将东西放在包外。如果没有.npmignore文件,但有一个.gitignore文件,然后将NPM忽略由相匹配的东西.gitignore文件。如果包含.gitignore文件排除的内容,可以创建一个空.npmignore文件来覆盖它。

In simpler terms, npm prefersthe .npmignorefile if it is there, but will fall back to the .gitignorefile.

简单来说,如果文件存在,npm更喜欢.npmignore文件,但会回退到该.gitignore文件。

In many cases, both Git and npm can ignore the same files, so it makes sense to just use a .gitignorefile on its own. If there's ever a discrepancy (i.e. npm and Git need to ignore different files), then you need to maintain separate .gitignoreand .npmignorefiles.

在许多情况下,Git 和 npm 都可以忽略相同的文件,因此单独使用一个.gitignore文件是有意义的。如果有任何差异(即 npm 和 Git 需要忽略不同的文件),那么您需要维护单独的.gitignore.npmignore文件。

More information on what to put in .npmignorefiles: Should I .npmignore my tests?

有关将什么放入.npmignore文件的更多信息:我应该 .npmignore 我的测试吗?

回答by Stewart Duffy

For anyone reading this trying to ignore a file/dir from git but wish to include it in npm publish and have tried using an empty .npmignorefile with no luck. This works.

对于任何阅读本文试图忽略来自 git 的文件/目录但希望将其包含在 npm publish 中并尝试使用空.npmignore文件但没有运气的人。这有效。

In your .gitignorefile, add the file/dir you wish to exclude **/buildfor example and in your .npmignorefile make sure you specify the same file/dir but with the !prefix so for the build example you would include !**/build

在您的.gitignore文件中,添加您希望排除的文件/目录**/build,例如,并在您的.npmignore文件中确保您指定相同的文件/目录但带有!前缀,因此对于构建示例,您将包括!**/build