gitignore nodejs :如何忽略node_modules

1,455 阅读2分钟

这篇文章讲述了如何在nodejs应用程序中添加.gitignore文件以及它的内容。.gitignore是一个包含文件名和文件夹路径条目的文件,在向nodejs项目提交源代码时,这些文件会被忽略。

哪些文件或文件夹在nodejs应用程序中被忽略?

  • 依赖关系
  • 日志文件
  • dotenv环境变量配置
  • serverless文件夹
  • IDE相关配置
  • 缓存文件
  • typecript和javascript相关文件

node_modules依赖项。在package.json中配置了很多依赖项,它们将被安装到项目的node_modules文件夹中,这些不需要提交到版本库。

在git ignore文件中添加node_modules路径如下,并且 jspm包也被忽略,如下图所示

node_modules/
jspm_packages/

bower_components的依赖性。

如果你使用bower软件包管理器,它会生成bower_components文件夹。

所以你必须添加以下条目

bower_components/

对于showpack的依赖,你必须添加web_modules文件夹,如下图所示

web_modules/

在nodejs中,有不同的软件包管理器。

npm是默认的软件包管理器,在node默认安装时就有。

它生成npm-debug.log

同样的,yarn会生成yarn-debug.log和yarn-error.log,lerna会生成lerna-debug.log。

所以,你必须在git忽略文件中添加所有这些日志文件,如下所示

npm-debug.log
yarn-error.log
yarn-debug.log
lerna-debug.log

dotenv环境变量文件

在节点应用程序中,如果你在环境文件中存储密匙,Dotenv文件提供了不需要提交到版本库的.env文件。

.env
.env.test
.custom-env

代码编辑器 vscode

vscode生成.vscode文件和vscod-test文件,用于vscode项目的导入和扩展工作。

缓存文件

有一些软件包管理器以及工具会生成temparory缓存文件,可以忽略不计,如下图所示

.yarn/cache
.yarn/unplugged
.yarn/install-state.gz
.eslintcache
.cache
.parcel-cache
.npm

下面是一个完整的.gitignore文件例子


node_modules/
jspm_packages/
bower_components/

npm-debug.log
yarn-error.log
yarn-debug.log
lerna-debug.log
.serverless

.env
.env.test
.custom-env

# cache files
.yarn/cache
.yarn/unplugged
.yarn/install-state.gz
.eslintcache
.cache
.parcel-cache
.npm

# vs editor related files
.vscode-test
.vscode

总之,gitignore是非常重要的文件,你需要知道它的内容是什么。