我应该在文件夹"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:
- 构建实时的Polyglot带有Node.js,Ruby,MongoDB和Socket.IO的应用程序
- https://github.com/mongolab/tractorpush-server (源)
- Building a Real-time, Polyglot Application with Node.js, Ruby, MongoDB and Socket.IO
- https://github.com/mongolab/tractorpush-server (source)
它指示我将文件夹 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
的文档:
如果您希望锁定软件包中包含的特定字节,例如对能够复制部署或构建有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.
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.
对于您部署的软件包,例如网站和应用程序,应使用npm收缩包装以锁定完整的依赖树:
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.
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屋!