在 heroku 中部署 Node.js 应用程序

标签 node.js heroku

我的 MEAN 应用的文件结构如下:

root
|---> public
      |----> css
      |----> js
              |----> controller
              |----> app.js
      |----> views
      |----> index.html

|---> app
      |----> server.js
|---> node_modules
|---> bower_components
|---> gulpfile.js
|---> package.json
|---> Procfile

在这个应用程序中,我使用 gulp 运行 public/index.html

gulpfile.js:

var gulp = require('gulp');
var browserSync = require('browser-sync');
var server = require('gulp-live-server');

gulp.task('server', function() {
     live = new server('app/server.js');
     live.start();
})
gulp.task('serve', ['server'], function() {
   browserSync.init({
      notify: false,
      port: process.env.PORT || 8080,
      server: {
        baseDir: ['public'],
        routes: {
            '/bower_components': 'bower_components'
        }
      }
   });
    gulp.watch(['public/**/*.*'])
        .on('change', browserSync.reload);
});

然后使用 REST APIapp 通信。这在本地机器上工作。我已将此项目上传到 heroku

我的个人资料:

web: node node_modules/gulp/bin/gulp serve

但是它显示错误。我在 heroku logs

中有以下错误

2017-05-21T16:26:57.305350+00:00 heroku[路由器]: at=error code=H10 desc="App crashed"method=GET path="/favicon.ico"host=myapp .herokuapp.com request_id=some-request-id fwd="fwd-ip"dyno=connect=service=status=503 bytes=protocol=https

2017-05-21T15:53:50.942664+00:00 app[web.1]: 错误:找不到模块 '/app/node_modules/gulp/bin/gulp'

我的 package.json 文件:

 {
    "name": "myapp",
    "scripts": {
       "test": "echo \"Error: no test specified\" && exit 1",
       "start": "gulp serve"
    },
    "repository": {
        "type": "git",
        "url": ""
    },
    "dependencies": {
        "async": "^2.4.0",
        "body-parser": "^1.17.2",
        "express": "^4.15.3",
        "mongoose": "^4.10.0",
        "morgan": "^1.8.1",
        "multer": "^1.3.0",
        "underscore": "^1.8.3"
    },
    "devDependencies": {
        "browser-sync": "^2.18.11",
        "gulp": "^3.9.1",
        "gulp-live-server": "0.0.30"
    }
}

有什么建议吗?提前致谢。

最佳答案

您可能在 package.json 文件中将 gulp 定义为开发依赖项(在 devDepenenies 下)。当 NODE_ENV 设置为 production 时,NPM 仅安装 devDependencies

当您部署到 heroku 时,NODE_ENV=production,因此永远不会安装 gulp。因此错误...

错误:找不到模块“/app/node_modules/gulp/bin/gulp”

只需将 gulp 和构建您的包所需的任何其他内容从 devDependencies 移动到 dependencies。你可以让 npm 为你移动它......

npm uninstall --save-dev gulp
npm install --save gulp

对构建 bundle 所需的每个开发依赖项重复此操作。或者您可以自己复制并粘贴它们。


这是一个没有理想解决方案 AFAIK 的常见问题。 NPM 期望在生产环境中,您已经预先构建了您的文件。就像您将它们发布到 NPM 一样。然而,在 heroku 和其他推送部署解决方案中,情况并非如此。

关于在 heroku 中部署 Node.js 应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44099411/

相关文章:

javascript - Node 类型错误: Promise resolver #<Promise> is not a function

javascript - 如何排除bundle中的js文件并在不同的 block /bundle中使用它?

node.js - 使用 mongoose .exec 有什么好处?

npm - 如何修复 `npm ci` 仅当你的 package.json 和 package-lock.json 或 npm-shrinkwrap.json 同步时才能安装包

node.js - 如何重新启动 Node.js 应用程序并将新进程移交给控制台

node.js - 多对多自关联 Sequelize

python - 安装 sqlite3 时 Heroku python 应用程序无法构建

python - 关系不存在 - Django 和 Postgres

postgresql - 重复主键,无法访问 psql 控制台

ruby-on-rails - heroku db :push 的点击问题