使用 Django 和 NPM 在 Heroku 上创建应用程序
我正在编写一个 Django 应用程序,其中包含一些 CoffeeScript。为了实现这一点,我使用 django-compressor 在应用程序启动之前将 CoffeeScript 编译为 JS。 django-compressor 要求机器上安装 NPM 来编译 CoffeeScript。
现在我想在 Heroku 上部署这个应用程序。我无法将 npm 放入我的 requirements.txt
中,所以我想知道如何在 Heroku 服务器上获取 npm?
I'm writing a Django app that includes some CoffeeScript in it. To allow for this I'm using django-compressor which compiles the CoffeeScript to JS before the app is launched. django-compressor requires that NPM is installed on the machine to compile the CoffeeScript.
Now I want to deploy this app on Heroku. I can't put npm in my requirements.txt
so I am wondering how I can get npm on the Heroku server?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
如果您想避免维护自定义构建包,可以使用多构建包。
使用 multi buildpack 非常简单:
heroku config:add BUILDPACK_URL=https://github.com/ddollar/heroku-buildpack-multi.git
https://github.com/heroku/heroku-buildpack-nodejs.git
https://github.com/heroku/heroku-buildpack-python.git
If you want to avoid maintaining a custom buildpack, you can use the multi buildpack.
Using the multi buildpack is super simple:
heroku config:add BUILDPACK_URL=https://github.com/ddollar/heroku-buildpack-multi.git
https://github.com/heroku/heroku-buildpack-nodejs.git
https://github.com/heroku/heroku-buildpack-python.git
npm install
注意:multi buildpack 是一种更好的实现方式这些天:)
我创建了官方Python heroku buildpack的一个分支,它允许使用可选的
npm_requirements.txt
来安装此类依赖项。我现在在heroku上使用coffeescript和less-css以及django-compressor:)
https://github。 com/jiaaro/heroku-buildpack-django
编辑:要从标准构建包切换到我的构建:
使用
heroku
命令行应用程序进行设置这BUILDPACK_URL
环境变量:Note: The multi buildpack is a much nicer way to accomplish this these days :)
I've created a fork of the official Python heroku buildpack that allows an optional
npm_requirements.txt
for installing such dependencies.I am now using coffeescript and less-css with django-compressor on heroku :)
https://github.com/jiaaro/heroku-buildpack-django
Edit: To switch to my buildback from the standard buildpack:
use the
heroku
command line app to set theBUILDPACK_URL
environment variable:您可以创建自己的 buildpack,混合 nodejs buildbpack 和 python构建包。或者在您的机器上编译 CoffeeScript 并将其放在 S3 上。
You can create your own buildpack, that mix nodejs buildbpack and python buildpack. Or compile your CoffeeScript on your machine and put it on S3.
我在为自己解决同样的问题时在谷歌中发现了这个问题。
我合并了两个官方构建包(python 和 nodejs),因此现在可以通过运行以下命令来拥有带有标准 npm-description 文件
package.json
的 Django 项目:的不同之处如下:
package.json
文件(至少应在此指定产品的名称和版本)文件)package.json
中列出I found this question in Google while solving the same problem for myself.
I merged two official buildpacks (python and nodejs), so now one can have Django project with standard npm-description file
package.json
by running this command:This solution differs from Jiaaro's one in the following:
package.json
file (at least name and version of your product should be specified in this file)package.json
@Jiaaro 的解决方案对我不起作用...导致一些奇怪的错误.../:
太累了,无法处理它,所以我环顾四周,发现了这个漂亮的资源:
-heroku-django 食谱
他们解释了如何添加自己的脚本来挂钩到heroku默认构建包。
工作起来就像一个魅力。 :)
@Jiaaro 's solution didn't work for me... Causes some weird error... /:
Was too tired to deal with it, so I looked around and I found this nifty resource:
- The heroku-django cookbook
They explain how you can add your own scripts that hook into heroku's default buildpacks.
Worked like a charm. :)
Heroku 领域的情况发生了变化
不再需要多重构建包、.builpack 文件或自定义构建包。只需将所需的官方 Heroku 构建包添加到您的 Heroku 应用程序中,它们就会按照输入的顺序执行。使用索引选项根据需要对它们重新排序。
也不需要 Gunt 任务、django-bower 等应用程序或其他占用服务器资源并减慢构建时间的专用工具。
您可以查看我关于如何无缝集成 Django + Bower + Heroku 的教程 这里。
Things have changed in Heroku land
There is no need for multi build packs, .builpack files, or custom build packs. Simply add the required official heroku build packs to your heroku app and they will execute in the order entered. Use the index option to reorder them as required.
There is also no need for, gunt tasks, apps like django-bower, or other specialized tools that take up server resources and slow build time.
You can check out my tutorial on how to seamlessly integrate Django + Bower + Heroku here.