javascript - 在 Heroku 上部署 NodeJS 应用程序时遇到错误

标签 javascript node.js heroku

我正在尝试部署我的 Nodejs 应用程序,但它给了我这个错误:

2019-11-25T18:16:16.927748+00:00 app[web.1]: > anonymous-forum-discussion@1.0.0 start /app
2019-11-25T18:16:16.927751+00:00 app[web.1]: > nodemon server.js
2019-11-25T18:16:16.927753+00:00 app[web.1]:
2019-11-25T18:16:16.935126+00:00 app[web.1]: sh: 1: nodemon: not found
2019-11-25T18:16:16.940397+00:00 app[web.1]: npm ERR! file sh
2019-11-25T18:16:16.940690+00:00 app[web.1]: npm ERR! code ELIFECYCLE
2019-11-25T18:16:16.940929+00:00 app[web.1]: npm ERR! errno ENOENT
2019-11-25T18:16:16.941180+00:00 app[web.1]: npm ERR! syscall spawn

我尝试了 StackOverflow 上发布的其他可能的解决方案,但它似乎根本不起作用。每次部署 Nodejs 应用程序时,我都会收到应用程序错误

我的package.json文件:

{
  "name": "anonymous-forum-discussion",
  "version": "1.0.0",
  "description": "Anonymous Forum Discussion",
  "main": "server.js",
  "scripts": {
    "start": "nodemon server.js",
    "test": "echo \"Error: no test specified\" && exit 1",
    "postinstall": "cd frontend && npm install && npm run build && cd .."
  },
  "dependencies": {
    "body-parser": "^1.19.0",
    "express": "^4.17.1",
    "mongoose": "^4.11.5"
  },
  "devDependencies": {
    "nodemon": "^1.19.4"
  },
  "engines": {
    "node": "10.15.3"
  }
}

我的Procfile:web:node server.js

我的server.js代码:

var express = require('express');
const path = require('path');

var mongoose = require('mongoose');
var bodyParser = require('body-parser');

var models = require('./api/models/message');

var routes = require('./api/routes/routes');

var port = process.env.PORT || 3001;
var app = express();
var Message = mongoose.model('Message')

mongoose.connect(process.env.MONGODB_URI || 'mongodb://localhost/discussion');

app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());

routes(app);

if (process.env.NODE_ENV === "production") {
  app.use(express.static("frontend/build"));
  console.log("production");
}

app.get('*', function (request, response){
  response.sendFile(path.resolve(__dirname, 'frontend/build', 'index.html'))
});

app.listen(port);

console.log('Server running on port ' + port);

如果有人能帮助我解决这个问题来解决 nodemon not found 错误,那就太好了。

最佳答案

我通过将 nodemon 替换为 node 解决了这个问题。

正如 @JDunken 指出的,nodemon 是一个开发工具。无法部署。

关于javascript - 在 Heroku 上部署 NodeJS 应用程序时遇到错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59038053/

相关文章:

ruby-on-rails - Angular 的 ng-src 在 Rails 生产环境中的使用

node.js - 使用 Redis Cloud 和 Heroku 正确配置 Node session 存储

Handlebar 中的 Javascript 函数调用

javascript - 如何通过浏览器确定远程服务器的延迟

javascript - 为什么 clickHandler 没有更新状态?

node.js - sinon 期望的不同参数/返回值

heroku - 无法在 Heroku 上部署具有 Godeps 依赖项的 go 应用程序

javascript - JQuery 中的可插入编辑组件

node.js - express.js 中的 "app.render"和 "res.render"有什么区别?

performance - nodejs http 和 redis,只有 6000req/s