在Heroku上创建node.js应用程序时,我应该检查node_modules进行git吗? [英] Should I check in node_modules to git when creating a node.js app on Heroku?

查看:93
本文介绍了在Heroku上创建node.js应用程序时,我应该检查node_modules进行git吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



https://devcenter.heroku.com/categories/nodejs



这些指令不会告诉你创建一个.gitignore node_modules,因此暗示node_modules应该签入git。当我在git中包含node_modules时,我的入门应用程序运行正常。



当我遵循更高级的示例时:

https://devcenter.heroku.com/articles / realtime-polyglot-app-node-ruby-mongodb-socketio
https:/ /github.com/mongolab/tractorpush-server (source)


它指示我将node_modules添加到.gitignore。所以我从git中删除了node_modules,将它添加到.gitignore,然后重新部署。这次部署失败如此:

  -----> Heroku接收推送
-----> Node.js应用检测到
----->解析引擎版本
使用Node.js版本:0.8.2
使用npm版本:1.0.106
----->获取Node.js二进制文件
----->将节点卖给slug
----->使用npm $ b $安装依赖关系错误:npm不适用于节点v0.8.2
必需: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)
在Module._compile(module.js:449:26)
在Object.Module._extensions。 (module.js:467:10)
在Module.load(module.js:356:32)
在Function.Module._load(module.js:312:12)
在Module.require(module.js:362:17)
at require(module.js:378:17)
在Object。< anonymous> (/tmp/node-npm-5iGk/cli.js:2:1)在Module._compile(module.js:449:26)处

错误:npm不适用于节点v0。 8.2
必需: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)
在Module._compile(module.js:449:26)
在Object.Module._extensions。 (module.js:467:10)
在Module.load(module.js:356:32)
在Function.Module._load(module.js:312:12)
在Module.require(module.js:362:17)
at require(module.js:378:17)
在Object。< anonymous> (/tmp/node-npm-5iGk/cli.js:2:1)在Module._compile(module.js:449:26)处

安装的依赖关系
----- >发现过程类型
Procfile声明类型 - > mongod,redis,web
----->编译的段塞大小为5.0MB
----->启动完成后,v9

运行heroku ps确认崩溃。好吧,没问题,所以我回滚了更改,将node_module添加回git存储库,并从.gitignore中删除它。但是,即使在恢复后,我仍然在部署时收到相同的错误消息,但现在应用程序再次正常运行。运行heroku ps告诉我应用程序正在运行。



所以我的问题是什么才是正确的做法?包含node_modules还是不包含?为什么我在回滚时仍然会收到错误消息?我的猜测是git版本库在Heroku方面处于不良状态?第二次更新

解决方案 b

常见问题解答不可用。



shrinkwrap
$ b


如果您想锁定在包中包含特定的字节,例如为了能够重现部署或构建100%的信心,那么你应该检查你的依赖到源代码控制,或追求一些其他机制,可以验证内容,而不是版本。

香农和史蒂文之前提到过这一点,但我认为,它应该是公认答案的一部分。






更新



以下建议的来源已更新。他们不再建议提交 node_modules 文件夹。


通常情况下,允许npm解析软件包的依赖关系。



对于您部署的软件包(如网站和应用程序),应该使用npm
shrinkwrap来锁定完全依赖tree:


https://docs.npmjs.com/cli/shrinkwrap







原始文章



作为参考,npm常见问题清楚地回答你的问题:


检查node_modules到git中, b $ b和应用程序。不要检查node_modules到git库和模块
打算重用。使用npm来管理dev
环境中的依赖关系,但不能在您的部署脚本中使用。


以及一些很好的理由这个,请阅读迈克尔罗杰斯在这篇






来源: https://docs.npmjs.com/misc/faq#should-i-check-my-node-modules-folder-into- git


I followed the basic getting started instructions for node.js on Heroku here:

https://devcenter.heroku.com/categories/nodejs

These instruction don't tell you to create a .gitignore node_modules, and therefore imply that node_modules should be checked in to git. When I include node_modules in git my getting started application ran correctly.

When I followed the more advanced example at:

https://devcenter.heroku.com/articles/realtime-polyglot-app-node-ruby-mongodb-socketio https://github.com/mongolab/tractorpush-server (source)

It instructed me to add node_modules to .gitignore. So I removed node_modules from git, added it to .gitignore, then re-deployed. 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

Running "heroku ps" confirms the crash. Ok, no problem, so I rolled back the change, add node_module back to the git repository and removed it from .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.

So my question is what's the right way to do this? Include 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.

From the documentation of shrinkwrap:

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 and Steven mentioned this before but I think, it should be part of the accepted answer.


Update

The source listed for the below recommendation has been updated. They are no longer recommending the node_modules folder be committed.

Usually, no. Allow npm to resolve dependencies for your packages.

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


Original Post

For reference, npm FAQ answers your question clearly:

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.


Source: https://docs.npmjs.com/misc/faq#should-i-check-my-node-modules-folder-into-git

这篇关于在Heroku上创建node.js应用程序时,我应该检查node_modules进行git吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆