如何将使用grunt的节点应用程序部署到heroku [英] How to deploy node app that uses grunt to heroku
问题描述
我使用的是grunt和grunt插件,比如 grunt-contrib-copy
, grunt-contrib-mincss
(列为我的应用程序的npm依赖项)。
另外,我不提交 npm_modules
文件夹和 public
文件夹,其中所有生成的文件都是。我无法弄清楚如何在部署和设置我的服务器(它已经在寻找 c> public)之后构建我的应用程序(我有
grunt build
命令) 文件夹)。
我看到了一些像 grunt-heroku-deploy
的东西,但在上传之前进行提交似乎是个不错的主意。也许有一些温和的决定......任何想法?
npm支持 postinstall
步骤(其中包括许多其他),可能正是您正在寻找的内容。
node.js heroku buildpack在您运行此命令时推送到heroku以解决构建依赖关系:
$ npm install --production
https: //devcenter.heroku.com/articles/nodejs-support#build-behavior
如果您查看npm文档,您可以设置一个系列脚本在任何人在运行 npm install
之前或之后运行。它在 package.json
的脚本
属性中配置。当包的生命周期中发生某些事情时,脚本
属性允许运行自定义脚本(包括 grunt
)。
例如,当任何人(包括Heroku)运行时,要回显一些文本并运行
,将它添加到你的 grunt
npm install package.json
:
{
...
脚本:{
postinstall:echo postinstall time; ./node_modules/grunt-cli/bin/grunt<您的任务名称>
},
...
}
https://npmjs.org/doc/scripts.html
重要提示:
postinstall
脚本,如果 grunt
命令没有执行,请检查错误输出。
grunt
和 grunt-cli
必须在您的<$ c中列为依赖项
$ c> package.json 所以它被Heroku安装。将它们列在 devDependencies
下是不够的,因为Heroku不会安装它们。另外,请注意,Heroku不会将其安装为全局包,因此要在Heroku上执行它,您将不得不使用相对路径(如上面配置)。
如果这不起作用(您可能需要小心摆弄相对路径),那么您可能需要考虑编写您自己的Heroku自定义buildpack 。
$ b
更新
从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 thegrunt
command doesn't execute. grunt
andgrunt-cli
must be listed as adependency
in yourpackage.json
so it gets installed by Heroku. Listing them underdevDependencies
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屋!