使用 grunt、bower、node 模块部署到 heroku

Posted

技术标签:

【中文标题】使用 grunt、bower、node 模块部署到 heroku【英文标题】:Deploying to heroku with grunt, bower, node modules 【发布时间】:2014-08-23 11:15:00 【问题描述】:

我正在向 heroku 部署一个平均应用程序,并且正在使用 bower 和 grunt。我应该在 git repo 中包含我的节点模块吗?如果没有,我真的不确定我需要如何部署,因为 post 脚本引用了节点模块中的凉亭安装。我应该在 git repo 中包含我的 bower_components 吗?我似乎不应该这样做,因为后记的重点不是在heroku上执行凉亭安装吗?但是当我这样做时,我得到一个错误。这是我的凉亭依赖项:

"dependencies": 
    "angular": "1.2.x",
    "angular-mocks": "~1.2.x",
    "jquery": "1.10.2",
    "bootstrap": "~3.1.1",
    "angular-bootstrap" : "0.11.0"
  

还有我的 package.json


  "main": "server.js",
  "engines": 
    "node": "0.10.29"
  ,
  "dependencies": 
    "express": "~3.4.x",
    "mongoose": "~3.6.x",
    "mongodb": "^1.4.0",
    "ejs": "*",
    "email-templates" : "1.0.0",
    "crypto": "0.0.3",
    "lodash": "~2.4.1",
    "aws-sdk": "2.0.0-rc11",
    "passport" : "~0.1.17",
    "passport-local" : "~0.1.6",
    "connect-flash" : "~0.1.1",
    "winston": "0.6.x",
    "gm"    : "1.16.0",
    "apn" : "1.5.2",
    "bcrypt-nodejs" : "latest",
    "angular-wizard": "latest",
    "point-in-polygon": "latest",
    "node-mailgun": "0.0.2",
    "nodemailer"  : "0.7.0",
    "passport-facebook" : "1.0.3"
  ,
  "devDependencies": 
    "supertest": "0.5.x",
    "bower": "0.6.x",
    "mocha": "1.8.x",
    "should": "1.2.x",
    "karma": "~0.10",
    "protractor": "^0.24.2",
    "http-server": "^0.6.1",
    "bower": "^1.3.1",
    "grunt-cli": "~0.1.13",
    "grunt-env": "~0.4.1",
    "grunt-node-inspector": "~0.1.3",
    "grunt-contrib-watch": "~0.6.1",
    "grunt-contrib-jshint": "~0.10.0",
    "grunt-contrib-csslint": "^0.2.0",
    "grunt-contrib-uglify": "~0.4.0",
    "grunt-contrib-cssmin": "~0.9.0",
    "grunt-nodemon": "~0.2.0",
    "grunt-concurrent": "~0.5.0",
    "grunt-mocha-test": "~0.10.0",
    "grunt-karma": "~0.8.2",
    "grunt-protractor-runner": "~1.0.0",
    "load-grunt-tasks": "~0.4.0",
    "jasmine-reporters": "~0.4.0",
    "karma": "~0.12.0",
    "karma-jasmine": "~0.2.1",
    "karma-coverage": "~0.2.0",
    "karma-chrome-launcher": "~0.1.2",
    "karma-firefox-launcher": "~0.1.3",
    "karma-phantomjs-launcher": "~0.1.2",
    "karma-junit-reporter": "~0.2",
    "shelljs": "^0.2.6",
    "xunit-file": "*"
  ,
  "scripts": 
    "postinstall": "node_modules/.bin/bower install",

    "pretest": "npm install",
    "test": "karma start test/karma.conf.js",
    "test-single-run": "karma start test/karma.conf.js  --single-run",

    "preupdate-webdriver": "npm install",
    "update-webdriver": "webdriver-manager update",

    "preprotractor": "npm run update-webdriver",
    "protractor": "protractor test/protractor-conf.js",

    "update-index-async": "node -e \"require('shelljs/global'); sed('-i', /\\/\\/@@NG_LOADER_START@@[\\s\\S]*\\/\\/@@NG_LOADER_END@@/, '//@@NG_LOADER_START@@\\n' + cat('bower_components/angular-loader/angular-loader.min.js') + '\\n//@@NG_LOADER_END@@', 'app/index-async.html');\""
  

我收到此错误:

   5816 info fsevents@0.2.0 Failed to exec install script
5817 error fsevents@0.2.0 install: `node-gyp rebuild`
5817 error Exit status 1
5818 error Failed at the fsevents@0.2.0 install script.
5818 error This is most likely a problem with the fsevents package,
5818 error not with npm itself.
5818 error Tell the author that this fails on your system:
5818 error     node-gyp rebuild
5818 error You can get their info via:
5818 error     npm owner ls fsevents
5818 error There is likely additional logging output above.
5819 error System Linux 3.8.11-ec2
5820 error command "/tmp/build_2cc32425-eae7-45af-8b95-104e1c2c7c53/vendor/node/bin/node" "/tmp/build_2cc32425-eae7-45af-8b95-104e1c2c7c53/vendor/node/bin/npm" "rebuild"
5821 error cwd /tmp/build_2cc32425-eae7-45af-8b95-104e1c2c7c53
5822 error node -v v0.10.29
5823 error npm -v 1.4.14
5824 error code ELIFECYCLE
5825 verbose exit [ 1, true ]

 !     Push rejected, failed to compile Node.js app

我读到我需要 npm install -g karma 但这无济于事。或者是凉亭安装出错...我的开发依赖项不正确吗?

【问题讨论】:

【参考方案1】:

将此添加到您的 .slugignore 文件中:

/node_modules/grunt-karma/
/node_modules/grunt-protractor-runner/
/node_modules/karma/
/node_modules/karma-chrome-launcher/
/node_modules/karma-mocha/
/node_modules/karma-ng-html2js-preprocessor/
/node_modules/ngmin/
/node_modules/protractor/
/node_modules/rfile/
/node_modules/rfileify/
/node_modules/rhtml/

这解决了我的 fsevents@0.2.0 问题。

【讨论】:

【参考方案2】:

您缺少用于 heroku 的 nodejs buildpack。此外,如果您使用 grunt,我认为您需要使用 grunt nodejs buildpack。就是这个:

https://github.com/mbuchetics/heroku-buildpack-nodejs-grunt

您只需要在 Gruntfile.js 中注册用于在 heroku 中构建的任务,例如:

grunt.registerTask('heroku:development', 'clean less mincss');
grunt.registerTask('heroku:production', 'clean less mincss uglify');

还要记得将配置变量添加到 heroku 应用程序:

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

你在 package.json 中使用了一个非常旧版本的 bower,我的是:

"scripts": 
    "postinstall": "./node_modules/bower/bin/bower install"
,
"dependencies": 
    "bower": "^1.3.5",

【讨论】:

【参考方案3】:

我发现 Joe Eames 的这个视频教程对于使用 MEAN 堆栈进行开发和部署到 Heroku 非常有用:http://pluralsight.com/training/courses/TableOfContents?courseName=building-angularjs-nodejs-apps-mean&highlight=

给出的指导是你不应该在 git 存储库中包含你的 node_modules,你应该让 Heroku 在你部署时为你构建这些。

通读您的文件,您似乎列出了太多依赖项。即我认为您不需要在 package.json 中将“Bower”列为开发依赖项。同样,您在 package.json 中包含的脚本不是我以前真正见过的。我将使用 Heroku 命令或此处找到的 grunt 构建包进行部署:github.com/mbuchetics/heroku-buildpack-nodejs-grunt.git

更多关于如何使用 grunt 部署到 Heroku 的描述可以在这里找到:https://medium.com/@3runjo/how-to-deploy-a-grunt-project-on-heroku-c227cb1ddc56

我知道这不是一个完整的答案,但应该给你足够的线索来调试你的问题。

【讨论】:

以上是关于使用 grunt、bower、node 模块部署到 heroku的主要内容,如果未能解决你的问题,请参考以下文章

bower

heroku上的咕噜声和凉亭

在 mac os 中安装 Grunt

停止 Heroku 运行 npm start + 运行啥来代替?

什么是bower

Grunt-injector 忽略来自 bower 依赖的 css 文件