Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
535 views
in Technique[技术] by (71.8m points)

node.js - Does NPM ignore files listed in .gitignore?

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

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

Is this enough to avoid uploading it on NPM registry?

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

If a project has both an .npmignore and .gitignore file, npm will only use the .npmignore file.

From the documentation:

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

In simpler terms, npm prefers the .npmignore file if it is there, but will fall back to the .gitignore file.

In many cases, both Git and npm can ignore the same files, so it makes sense to just use a .gitignore file 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 .gitignore and .npmignore files.

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...