如何将使用 grunt 的节点应用程序部署到 heroku [英] How to deploy node app that uses grunt to heroku

查看:17
本文介绍了如何将使用 grunt 的节点应用程序部署到 heroku的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 grunt 和 grunt 插件,例如 grunt-contrib-copygrunt-contrib-mincss(列为我的应用程序的 npm 依赖项).

此外,我不提交所有生成的文件所在的 npm_modules 文件夹和 public 文件夹.在部署和设置我的服务器(它已经在寻找 public 文件夹)之后,我无法弄清楚如何构建我的应用程序(我有 grunt build 命令).>

我看到了一些类似 grunt-heroku-deploy 的东西,但在上传之前提交似乎是个坏主意.也许有一些温和的决定......有什么想法吗?

解决方案

npm 支持 postinstall 步骤(以及许多其他步骤),这可能正是您正在寻找的.>

当您推送到 heroku 以解决构建依赖项时,node.js heroku buildpack 会运行此命令:

$ npm install --production

https://devcenter.heroku.com/articles/nodejs-support#构建行为

如果您查看 npm 文档,您可以设置一系列脚本以在任何人为您的包运行 npm install 之前或之后运行.它在 package.jsonscripts 属性中配置.scripts 属性允许在包的生命周期中发生某些事情时运行自定义脚本(包括 grunt).

例如,要在任何人(包括 Heroku)运行 npm install 时回显一些文本并运行 grunt 命令,请将其添加到您的 package.json:

<代码>{...脚本":{"postinstall": "echo postinstall time; ./node_modules/grunt-cli/bin/grunt <你的任务名称>"},...}

https://npmjs.org/doc/scripts.html

重要警告:

  • 您可能需要在 postinstall 脚本中更改 grunt 二进制文件的路径,如果 grunt 命令未执行,请检查错误输出.
  • gruntgrunt-cli 必须在您的 package.json 中列为 dependency 以便它得到由 Heroku 安装.将它们列在 devDependencies 下是不够的,因为 Heroku 不会安装它们.另请注意,Heroku 不会将其安装为全局包,因此要在 Heroku 上执行它,您将不得不使用相对路径(如上面配置的那样).

如果这不起作用(您可能需要稍微摆弄相对路径),那么您可能需要考虑编写 您自己的 Heroku 自定义构建包.

更新

从 0.4 开始,grunt 包不再包含 grunt 二进制文件,它现在是 grunt-cli 包的一部分.答案已更新以反映这一点.

I'm using grunt and also grunt plugins like grunt-contrib-copy, grunt-contrib-mincss (that listed as npm dependencies for my application).

Also I don't commit npm_modules folder and public folder, where all generated files are. And I can't figure out how to build my app (I have grunt build command) after deploy and setup my server (it's already looking for public folder).

I saw some stuff like grunt-heroku-deploy, but it seems me a bad idea to commit before upload. Maybe there are some gentle decisions... Any thoughts?

解决方案

npm has a support for a postinstall step (among many others) that might be just what you're looking for.

The node.js heroku buildpack runs this command when you push to heroku to resolve build dependencies:

$ npm install --production

https://devcenter.heroku.com/articles/nodejs-support#build-behavior

If you take a look at the npm documentation, you can setup a series of scripts to run either before or after anyone runs npm install for your package. It's configured in the scripts property of package.json. The scripts property allows to run custom scripts (including grunt) when certain things happen in a package's lifecycle.

For example, to echo some text and run the grunt command whenever anyone (including Heroku) runs npm install, add this to your package.json:

{
  ...
  "scripts": {
    "postinstall": "echo postinstall time; ./node_modules/grunt-cli/bin/grunt <your task name>"
  },
  ...
}

https://npmjs.org/doc/scripts.html

Important caveats:

  • You might have to change the path to the grunt binary in the postinstall script, check the error output if the grunt command doesn't execute.
  • grunt and grunt-cli must be listed as a dependency in your package.json so it gets installed by Heroku. Listing them under devDependencies is not sufficient since Heroku won't install those. Also, note that Heroku won't install it as a global package so to execute it on Heroku you're going to have to use a relative path (as it is configured above).

If this doesn't work (you'll probably need to fiddle with the relative paths a bit), then you might want to consider writing your own custom buildpack for Heroku.

Update

As of 0.4, the grunt package no longer contains the grunt binary, which is now part of the grunt-cli package. The answer has been updated to reflect this.

这篇关于如何将使用 grunt 的节点应用程序部署到 heroku的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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