Heroku上的grunt和bower

9
今天我想把我的node.js应用程序部署到Heroku。在本地开发中,我也使用bowergrunt,并希望继续使用它们。
现在,我按照网上的一些建议将bower添加到我的dependencies,并将"postinstall": "node_modules/.bin/bower install"添加到我的package.json postinstall中。
现在,我遇到了一些grunt的问题。当我将我的grunt依赖从devDependencies转移到dependencies时,grunt无法识别我的tasks,例如当我有:
    {
      "name": "js-linkedin-connector",
      "version": "0.1.0",
      "dependencies": {
        "passport": "~0.1.17",
        "passport-local": "~0.1.6",
        "passport-linkedin-oauth2": "~1.0.1",
        "connect": "~2.11.0",
        "underscore": "~1.5.2",
        "bower": "1.2.x",
        "grunt": "~0.4.1",
        "grunt-cli": "0.1.11",
        "load-grunt-tasks": "~0.1.0",
        "grunt-contrib-copy": "~0.4.1",
        "grunt-contrib-concat": "~0.3.0",
        "grunt-contrib-coffee": "~0.7.0",
        "grunt-contrib-uglify": "~0.2.0",
        "grunt-contrib-compass": "~0.5.0",
        "grunt-contrib-jshint": "~0.6.0",
        "grunt-contrib-cssmin": "~0.6.0",
        "grunt-contrib-connect": "~0.5.0",
        "grunt-contrib-clean": "~0.5.0",
        "grunt-contrib-htmlmin": "~0.1.3",
        "grunt-contrib-watch": "~0.5.2",
        "grunt-autoprefixer": "~0.2.0",
        "grunt-usemin": "~2.0.0",
        "grunt-svgmin": "~0.2.0",
        "grunt-rev": "~0.1.0",
        "grunt-concurrent": "~0.3.0",
        "grunt-jasmine-node": "~0.1.0",
        "grunt-google-cdn": "~0.2.0",
        "grunt-ngmin": "~0.0.2",
        "time-grunt": "~0.1.0",
        "grunt-karma": "~0.6.2",
        "connect-livereload": "~0.3.0"
      },
      "devDependencies": {
        "karma-ng-scenario": "~0.1.0",
        "karma-script-launcher": "~0.1.0",
        "karma-chrome-launcher": "~0.1.0",
        "karma-firefox-launcher": "~0.1.0",
        "karma-html2js-preprocessor": "~0.1.0",
        "karma-jasmine": "~0.1.3",
        "karma-requirejs": "~0.1.0",
        "karma-phantomjs-launcher": "~0.1.0",
        "karma": "~0.10.4",
        "karma-ng-html2js-preprocessor": "~0.1.0"
      },
      "engines": {
        "node": ">=0.8.0",
        "npm": "1.3.x"
      },
      "scripts": {
        "test": "grunt test",
        "postinstall": "node_modules/.bin/bower install;node_modules/.bin/grunt server:dist"
      }
    }

在我的package.json中,当我输入./node_modules/.bin/grunt时,出现以下情况:

Warning: Task "jshint" not found. Use --force to continue.

Aborted due to warnings.

但是当我将 grunt 的依赖项移到 devDependencies 中时:

    {
      "name": "js-linkedin-connector",
      "version": "0.1.0",
      "dependencies": {
        "passport": "~0.1.17",
        "passport-local": "~0.1.6",
        "passport-linkedin-oauth2": "~1.0.1",
        "connect": "~2.11.0",
        "underscore": "~1.5.2",
        "bower": "1.2.x"
      },
      "devDependencies": {
        "grunt": "~0.4.1",
        "grunt-cli": "0.1.11",
        "load-grunt-tasks": "~0.1.0",
        "grunt-contrib-copy": "~0.4.1",
        "grunt-contrib-concat": "~0.3.0",
        "grunt-contrib-coffee": "~0.7.0",
        "grunt-contrib-uglify": "~0.2.0",
        "grunt-contrib-compass": "~0.5.0",
        "grunt-contrib-jshint": "~0.6.0",
        "grunt-contrib-cssmin": "~0.6.0",
        "grunt-contrib-connect": "~0.5.0",
        "grunt-contrib-clean": "~0.5.0",
        "grunt-contrib-htmlmin": "~0.1.3",
        "grunt-contrib-watch": "~0.5.2",
        "grunt-autoprefixer": "~0.2.0",
        "grunt-usemin": "~2.0.0",
        "grunt-svgmin": "~0.2.0",
        "grunt-rev": "~0.1.0",
        "grunt-concurrent": "~0.3.0",
        "grunt-jasmine-node": "~0.1.0",
        "grunt-google-cdn": "~0.2.0",
        "grunt-ngmin": "~0.0.2",
        "time-grunt": "~0.1.0",
        "grunt-karma": "~0.6.2",
        "connect-livereload": "~0.3.0",
        "karma-ng-scenario": "~0.1.0",
        "karma-script-launcher": "~0.1.0",
        "karma-chrome-launcher": "~0.1.0",
        "karma-firefox-launcher": "~0.1.0",
        "karma-html2js-preprocessor": "~0.1.0",
        "karma-jasmine": "~0.1.3",
        "karma-requirejs": "~0.1.0",
        "karma-phantomjs-launcher": "~0.1.0",
        "karma": "~0.10.4",
        "karma-ng-html2js-preprocessor": "~0.1.0"
      },
      "engines": {
        "node": ">=0.8.0",
        "npm": "1.3.x"
      },
      "scripts": {
        "test": "grunt test",
        "postinstall": "node_modules/.bin/bower install;node_modules/.bin/grunt server:dist"
      }
    }

一切都正常。

问题是什么,如何解决才能将我的 grunt + bower 应用程序部署到 Heroku?

2个回答

15

将你的Grunt依赖项保留在devDependencies中,这是它们所属的地方。使用自定义构建包允许Heroku在其平台上执行你的Grunt流程。

一个示例应用程序展示了如何完全做到这一点

构建包可以让你以不同的方式启动应用程序,在这种情况下,你需要从配置设置中更改它:

heroku config:set BUILDPACK_URL=https://github.com/mbuchetics/heroku-buildpack-nodejs-grunt.git

接下来只需将一个heroku任务添加到您的Grunt配置中即可。


谢谢,我今天稍后会去看一下。 - Andna
3
我按照您的建议使用了这个构建包,一切都很好,应用程序已经成功部署:),谢谢。 - Andna
2
@Nico能否详细解释一下将它们留在devDependencies中“属于的地方”是什么意思?如果你需要在生产服务中使用它们,那么它们就不再是开发依赖了。你提到的构建组件也建议将grunt放入dependencies中。 - Jason Farnsworth
@Nico 谢谢。我今天在 SO 上闲逛,这个解决了我的问题。+1,如果你有机会来明尼阿波利斯,我请你喝第一杯啤酒。 - sgmeyer
我遇到了类似的问题。不得不将Grunt和Bower移到依赖项(dependencies)中而不是开发依赖(devDependencies)中。更多信息请参见:https://github.com/mbuchetics/heroku-buildpack-nodejs-grunt/issues/5 - Kyle
2
官方的构建已经更新。以下页面上的说明对我有效:https://devcenter.heroku.com/articles/nodejs-support - Ian

5

实际上,我尝试了一些方法来确定对我有效的方法。我需要使用uglify压缩我的脚本。

  1. First, I added grunt and grunt-contrib-uglify to my devDependencies (as I develop on my machine), and tried the "postinstall" script in package.json:

    "scripts": {
      "start": "node index.js",
      "postinstall": "grunt uglify"
    },
    "devDependencies": {
      "grunt": "~0.4.4",
      "grunt-contrib-uglify": "^0.4.0"
    }
    

    this obviously did not work, because heroku does not install devDependencies.

  2. after this, I set up the buildpack, added a heroku task to my Gruntfile.js, removed the postinstall script and tried to push.

    This was also unsuccessful, because the buildpack only installed grunt-cli and grunt, but not grunt-contrib-uglify.

  3. next I moved grunt & grunt-contrib-uglify to dependencies from devDependencies. (if I move uglify, why should I not move grunt). This way it worked, but made me feel: why do I need the buildpack, if I have to add grunt to dependencies?

  4. I've reverted to the default buildpack

    heroku config:unset BUILDPACK_URL
    

    and added the postinstall script mentioned above. this was not working, because the grunt command was still missing.

  5. finally, I also added grunt-cli to dependencies, and it works now, without the buildpack!

结论:您不需要构建包 - 将您的grunt依赖项添加到“依赖项”中即可。 或者,构建包应该修复此问题:https://github.com/mbuchetics/heroku-buildpack-nodejs-grunt/issues/5

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接