我应该检查文件夹“node_modules"吗?在 Heroku 上创建 Node.js 应用程序时使用 Git? [英] Should I check in folder "node_modules" to Git when creating a Node.js app on Heroku?
问题描述
我在此处遵循 Heroku 上 Node.js 的基本入门说明:
I followed the basic getting started instructions for Node.js on Heroku here:
https://devcenter.heroku.com/categories/nodejs
这些说明不会告诉您创建 .gitignore node_modules,因此暗示应该将文件夹 node_modules 签入 Git.当我在 Git 存储库中包含 node_modules 时,我的入门应用程序运行正常.
These instruction don't tell you to create a .gitignore node_modules, and therefore imply that folder node_modules should be checked in to Git. When I included node_modules in Git repository, my getting started application ran correctly.
当我遵循更高级的示例时:
When I followed the more advanced example at:
它指示我将文件夹 node_modules 添加到文件 .gitignore.所以我从 Git 中删除了文件夹 node_modules,将它添加到文件 .gitignore,然后重新部署.这次部署失败了:
It instructed me to add folder node_modules to file .gitignore. So I removed folder node_modules from Git, added it to file .gitignore, and then redeployed. This time the deployed failed like so:
-----> Heroku receiving push
-----> Node.js app detected
-----> Resolving engine versions
Using Node.js version: 0.8.2
Using npm version: 1.0.106
-----> Fetching Node.js binaries
-----> Vendoring node into slug
-----> Installing dependencies with npm
Error: npm doesn't work with node v0.8.2
Required: node@0.4 || 0.5 || 0.6
at /tmp/node-npm-5iGk/bin/npm-cli.js:57:23
at Object.<anonymous> (/tmp/node-npm-5iGk/bin/npm-cli.js:77:3)
at Module._compile (module.js:449:26)
at Object.Module._extensions..js (module.js:467:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Module.require (module.js:362:17)
at require (module.js:378:17)
at Object.<anonymous> (/tmp/node-npm-5iGk/cli.js:2:1)
at Module._compile (module.js:449:26)
Error: npm doesn't work with node v0.8.2
Required: node@0.4 || 0.5 || 0.6
at /tmp/node-npm-5iGk/bin/npm-cli.js:57:23
at Object.<anonymous> (/tmp/node-npm-5iGk/bin/npm-cli.js:77:3)
at Module._compile (module.js:449:26)
at Object.Module._extensions..js (module.js:467:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Module.require (module.js:362:17)
at require (module.js:378:17)
at Object.<anonymous> (/tmp/node-npm-5iGk/cli.js:2:1)
at Module._compile (module.js:449:26)
Dependencies installed
-----> Discovering process types
Procfile declares types -> mongod, redis, web
-----> Compiled slug size is 5.0MB
-----> Launching... done, v9
运行heroku ps"确认崩溃.好的,没问题,所以我回滚了更改,将文件夹 node_module 添加回 Git 存储库并将其从文件 .gitignore 中删除.但是,即使在恢复之后,我仍然在部署时收到相同的错误消息,但现在应用程序再次正常运行.运行heroku ps"告诉我应用程序正在运行.
Running "heroku ps" confirms the crash. OK, no problem, so I rolled back the change, added folder node_module back to the Git repository and removed it from file .gitignore. However, even after reverting, I still get the same error message on deploy, but now the application is running correctly again. Running "heroku ps" tells me the application is running.
这样做的正确方法是什么?是否包含文件夹node_modules?为什么在回滚时仍然会收到错误消息?我的猜测是 Git 存储库在 Heroku 方面处于错误状态.
What's the right way to do this? Include folder node_modules or not? And why would I still be getting the error message when I rollback? My guess is the Git repository is in a bad state on the Heroku side.
推荐答案
第二次更新
常见问题解答不再可用.
Second Update
The FAQ is not available anymore.
来自 shrinkwrap
的文档:
From the documentation of shrinkwrap
:
如果您希望锁定包中包含的特定字节,例如对能够重现部署或构建有 100% 的信心,那么您应该将您的依赖项检查到源代码控制中,或采用其他一些机制可以验证内容而不是版本.
If you wish to lock down the specific bytes included in a package, for example to have 100% confidence in being able to reproduce a deployment or build, then you ought to check your dependencies into source control, or pursue some other mechanism that can verify contents rather than versions.
Shannon 和 Steven 之前提到过这一点,但我认为这应该是公认答案的一部分.
Shannon and Steven mentioned this before but I think it should be part of the accepted answer.
为以下推荐列出的来源已更新.他们不再建议提交 node_modules
文件夹.
The source listed for the below recommendation has been updated. They are no longer recommending the node_modules
folder be committed.
通常不会.允许 npm 为您的包解析依赖项.
Usually, no. Allow npm to resolve dependencies for your packages.
对于您部署的包,例如网站和应用程序,您应该使用 npmshrinkwrap 来锁定你的完整依赖树:
For packages you deploy, such as websites and apps, you should use npm shrinkwrap to lock down your full dependency tree:
https://docs.npmjs.com/cli/shrinkwrap
原帖
作为参考,npm FAQ 清楚地回答了您的问题:
Original Post
For reference, npm FAQ answers your question clearly:
将 node_modules 检查到 git 中以获取您部署的内容,例如网站和应用程序.不要将 node_modules 检入 git 中的库和模块旨在重复使用.使用 npm 管理开发中的依赖项环境,但不在您的部署脚本中.
Check node_modules into git for things you deploy, such as websites and apps. Do not check node_modules into git for libraries and modules intended to be reused. Use npm to manage dependencies in your dev environment, but not in your deployment scripts.
并为此提供一些好的理由,请阅读 Mikeal Rogers 的帖子.
and for some good rationale for this, read Mikeal Rogers' post on this.
来源:https://docs.npmjs.com/misc/faq#should-i-check-my-node-modules-folder-into-git
这篇关于我应该检查文件夹“node_modules"吗?在 Heroku 上创建 Node.js 应用程序时使用 Git?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!