首页 > 解决方案 > package.json 文件修改部署

问题描述

在 heroku 上部署我的 nodejs 应用程序时遇到问题。我根据文档修改了根文件下的文件,但没有成功。

这是我的 package.json 的样子:

   {
  "name": "appName",
  "version": "1.0.0",
  "main": "server.js",
  "scripts": {
    "client": "cd client && yarn start",
    "server": "nodemon server.js",
    "dev": "concurrently --kill-others-on-fail \"yarn server\" \"yarn client\"",
    "start": "node server.js",
    "test": "node test.js"
  },
  "dependencies": {
    "body-parser": "^1.18.3",
    "express": "^4.16.4",
    "mongodb": "^3.1.10"
  },
  "devDependencies": {
    "concurrently": "^4.0.1"
  },
  "keywords": [
    "node",
    "heroku",
    "express"
  ],
}

我添加了 Procfile,内容为web: node server.js Here is how app.json 文件的样子

{
  "name": "Start on Heroku: Node.js",
  "description": "A barebones Node.js app using Express 4",
  "logo": "https://cdn.rawgit.com/heroku/node-js-getting-started/master/public/node.svg",
  "keywords": ["node", "express", "heroku"],

}

这是文件结构:

在此处输入图像描述

该应用程序仅yarn dev在本地设备上使用 cmd 运行。当我在线托管应用程序时,出现以下错误:

2019-01-09T00:44:06.643530+00:00 app[web.1]: 
2019-01-09T00:44:06.644740+00:00 app[web.1]: npm ERR! A complete log of this run can be found in:
2019-01-09T00:44:06.645394+00:00 app[web.1]: npm ERR!     /app/.npm/_logs/2019-01-09T00_44_06_514Z-debug.log
2019-01-09T00:44:06.735957+00:00 heroku[web.1]: State changed from starting to crashed
2019-01-09T00:44:06.716170+00:00 heroku[web.1]: Process exited with status 1
2019-01-09T03:33:44.388964+00:00 heroku[web.1]: State changed from crashed to starting
2019-01-09T03:33:56.753892+00:00 heroku[web.1]: Starting process with command `npm start`
2019-01-09T03:34:00.819968+00:00 heroku[web.1]: Process exited with status 1
2019-01-09T03:34:00.719009+00:00 app[web.1]: internal/modules/cjs/loader.js:583
2019-01-09T03:34:00.719055+00:00 app[web.1]: throw err;
2019-01-09T03:34:00.719058+00:00 app[web.1]: ^
2019-01-09T03:34:00.719059+00:00 app[web.1]: 
2019-01-09T03:34:00.719062+00:00 app[web.1]: Error: Cannot find module 'mongodb'
2019-01-09T03:34:00.719071+00:00 app[web.1]: at Function.Module._resolveFilename (internal/modules/cjs/loader.js:581:15)
2019-01-09T03:34:00.719074+00:00 app[web.1]: at Function.Module._load (internal/modules/cjs/loader.js:507:25)
2019-01-09T03:34:00.719093+00:00 app[web.1]: at Module.require (internal/modules/cjs/loader.js:637:17)
2019-01-09T03:34:00.719096+00:00 app[web.1]: at require (internal/modules/cjs/helpers.js:22:18)
2019-01-09T03:34:00.719098+00:00 app[web.1]: at Object.<anonymous> (/app/server.js:3:21)
2019-01-09T03:34:00.719101+00:00 app[web.1]: at Module._compile (internal/modules/cjs/loader.js:689:30)
2019-01-09T03:34:00.719103+00:00 app[web.1]: at Object.Module._extensions..js (internal/modules/cjs/loader.js:700:10)
2019-01-09T03:34:00.719104+00:00 app[web.1]: at Module.load (internal/modules/cjs/loader.js:599:32)
2019-01-09T03:34:00.719106+00:00 app[web.1]: at tryModuleLoad (internal/modules/cjs/loader.js:538:12)
2019-01-09T03:34:00.719108+00:00 app[web.1]: at Function.Module._load (internal/modules/cjs/loader.js:530:3)
2019-01-09T03:34:00.735003+00:00 app[web.1]: npm ERR! code ELIFECYCLE
2019-01-09T03:34:00.735008+00:00 app[web.1]: npm ERR! errno 1
2019-01-09T03:34:00.735011+00:00 app[web.1]: npm ERR! locate_physician@1.0.0 start: `node server.js`
2019-01-09T03:34:00.735013+00:00 app[web.1]: npm ERR! Exit status 1
2019-01-09T03:34:00.735015+00:00 app[web.1]: npm ERR!
2019-01-09T03:34:00.735017+00:00 app[web.1]: npm ERR! Failed at the locate_physician@1.0.0 start script.
2019-01-09T03:34:00.735020+00:00 app[web.1]: npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
2019-01-09T03:34:00.751015+00:00 app[web.1]: 
2019-01-09T03:34:00.751020+00:00 app[web.1]: npm ERR! A complete log of this run can be found in:
2019-01-09T03:34:00.751022+00:00 app[web.1]: npm ERR!     /app/.npm/_logs/2019-01-09T03_34_00_737Z-debug.log
2019-01-09T09:25:56.872025+00:00 heroku[web.1]: State changed from crashed to starting
2019-01-09T09:26:13.863271+00:00 heroku[web.1]: State changed from starting to crashed
2019-01-09T09:26:13.839786+00:00 heroku[web.1]: Process exited with status 1
2019-01-09T15:12:04.203792+00:00 heroku[web.1]: State changed from crashed to starting
2019-01-09T15:12:16.571688+00:00 heroku[web.1]: Starting process with command `npm start`
2019-01-09T15:12:19.707280+00:00 app[web.1]: 
2019-01-09T15:12:19.707306+00:00 app[web.1]: > locate_physician@1.0.0 start /app
2019-01-09T15:12:19.707308+00:00 app[web.1]: > node server.js
2019-01-09T15:12:19.707309+00:00 app[web.1]: 
2019-01-09T15:12:20.225456+00:00 app[web.1]: internal/modules/cjs/loader.js:583
2019-01-09T15:12:20.225500+00:00 app[web.1]: throw err;
2019-01-09T15:12:20.225502+00:00 app[web.1]: ^
2019-01-09T15:12:20.225504+00:00 app[web.1]: 
2019-01-09T15:12:20.225506+00:00 app[web.1]: Error: Cannot find module 'mongodb'
2019-01-09T15:12:20.225509+00:00 app[web.1]: at Function.Module._resolveFilename (internal/modules/cjs/loader.js:581:15)
2019-01-09T15:12:20.225511+00:00 app[web.1]: at Function.Module._load (internal/modules/cjs/loader.js:507:25)
2019-01-09T15:12:20.225514+00:00 app[web.1]: at Module.require (internal/modules/cjs/loader.js:637:17)
2019-01-09T15:12:20.225516+00:00 app[web.1]: at require (internal/modules/cjs/helpers.js:22:18)
2019-01-09T15:12:20.225517+00:00 app[web.1]: at Object.<anonymous> (/app/server.js:3:21)
2019-01-09T15:12:20.225519+00:00 app[web.1]: at Module._compile (internal/modules/cjs/loader.js:689:30)
2019-01-09T15:12:20.225521+00:00 app[web.1]: at Object.Module._extensions..js (internal/modules/cjs/loader.js:700:10)
2019-01-09T15:12:20.225523+00:00 app[web.1]: at Module.load (internal/modules/cjs/loader.js:599:32)
2019-01-09T15:12:20.225525+00:00 app[web.1]: at tryModuleLoad (internal/modules/cjs/loader.js:538:12)
2019-01-09T15:12:20.225527+00:00 app[web.1]: at Function.Module._load (internal/modules/cjs/loader.js:530:3)
2019-01-09T15:12:20.236484+00:00 app[web.1]: npm ERR! code ELIFECYCLE
2019-01-09T15:12:20.236488+00:00 app[web.1]: npm ERR! errno 1
2019-01-09T15:12:20.236490+00:00 app[web.1]: npm ERR! locate_physician@1.0.0 start: `node server.js`
2019-01-09T15:12:20.236492+00:00 app[web.1]: npm ERR! Exit status 1
2019-01-09T15:12:20.236494+00:00 app[web.1]: npm ERR!
2019-01-09T15:12:20.236496+00:00 app[web.1]: npm ERR! Failed at the locate_physician@1.0.0 start script.
2019-01-09T15:12:20.244466+00:00 app[web.1]: npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
2019-01-09T15:12:20.259988+00:00 app[web.1]: 
2019-01-09T15:12:20.260851+00:00 app[web.1]: npm ERR! A complete log of this run can be found in:
2019-01-09T15:12:20.261205+00:00 app[web.1]: npm ERR!     /app/.npm/_logs/2019-01-09T15_12_20_244Z-debug.log
2019-01-09T15:12:20.331271+00:00 heroku[web.1]: Process exited with status 1
2019-01-09T20:48:25.604189+00:00 heroku[web.1]: State changed from crashed to starting
2019-01-09T20:48:37.999462+00:00 heroku[web.1]: State changed from starting to crashed
2019-01-09T20:48:37.911887+00:00 app[web.1]: npm ERR! code ELIFECYCLE
2019-01-09T20:48:37.913316+00:00 app[web.1]: npm ERR! errno 1
2019-01-09T20:48:37.914017+00:00 app[web.1]: npm ERR! locate_physician@1.0.0 start: `node server.js`
2019-01-09T20:48:37.914183+00:00 app[web.1]: npm ERR! Exit status 1
2019-01-09T20:48:37.914440+00:00 app[web.1]: npm ERR!
2019-01-09T20:48:37.914621+00:00 app[web.1]: npm ERR! Failed at the locate_physician@1.0.0 start script.
2019-01-09T20:48:37.914798+00:00 app[web.1]: npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
2019-01-09T20:48:37.921811+00:00 app[web.1]: 
2019-01-09T20:48:37.922035+00:00 app[web.1]: npm ERR! A complete log of this run can be found in:
2019-01-09T20:48:37.922242+00:00 app[web.1]: npm ERR!     /app/.npm/_logs/2019-01-09T20_48_37_915Z-debug.log
2019-01-09T20:48:37.660555+00:00 app[web.1]: 
2019-01-09T20:48:37.660575+00:00 app[web.1]: > locate_physician@1.0.0 start /app
2019-01-09T20:48:37.660577+00:00 app[web.1]: > node server.js
2019-01-09T20:48:37.660579+00:00 app[web.1]: 
2019-01-09T20:48:37.901555+00:00 app[web.1]: internal/modules/cjs/loader.js:583
2019-01-09T20:48:37.901558+00:00 app[web.1]: throw err;
2019-01-09T20:48:37.901559+00:00 app[web.1]: ^
2019-01-09T20:48:37.901561+00:00 app[web.1]: 
2019-01-09T20:48:37.901562+00:00 app[web.1]: Error: Cannot find module 'mongodb'
2019-01-09T20:48:37.901578+00:00 app[web.1]: at Function.Module._resolveFilename (internal/modules/cjs/loader.js:581:15)
2019-01-09T20:48:37.901579+00:00 app[web.1]: at Function.Module._load (internal/modules/cjs/loader.js:507:25)
2019-01-09T20:48:37.901580+00:00 app[web.1]: at Module.require (internal/modules/cjs/loader.js:637:17)
2019-01-09T20:48:37.901581+00:00 app[web.1]: at require (internal/modules/cjs/helpers.js:22:18)
2019-01-09T20:48:37.901583+00:00 app[web.1]: at Object.<anonymous> (/app/server.js:3:21)
2019-01-09T20:48:37.901584+00:00 app[web.1]: at Module._compile (internal/modules/cjs/loader.js:689:30)
2019-01-09T20:48:37.901585+00:00 app[web.1]: at Object.Module._extensions..js (internal/modules/cjs/loader.js:700:10)
2019-01-09T20:48:37.901586+00:00 app[web.1]: at Module.load (internal/modules/cjs/loader.js:599:32)
2019-01-09T20:48:37.901587+00:00 app[web.1]: at tryModuleLoad (internal/modules/cjs/loader.js:538:12)
2019-01-09T20:48:37.901588+00:00 app[web.1]: at Function.Module._load (internal/modules/cjs/loader.js:530:3)
2019-01-09T23:46:16.430562+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/favicon.ico" host=passdemoo.herokuapp.com request_id=eb70b3af-f8a8-4de6-8d60-5b9d6977dc11 fwd="134.87.187.106" dyno= connect= service= status=503 bytes= protocol=https

更新:

Here is my server.js file

const express = require('express');
const bodyParser = require('body-parser');
const MongoClient = require('mongodb').MongoClient;
const app = express();
const port = process.env.PORT || 5000;
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));

app.get('*', (req, res) => {

const uri = "mongodb+srv://ve:pass@addressbook-1l4ir.mongodb.net/admin"
MongoClient.connect(uri, { useNewUrlParser: true },function(err, client) {
   if(err) {
        console.log('Error occurred while connecting to MongoDB Atlas...\n',err);
   }
   console.log('Connected...');

   const collection = client.db("contactInfo").collection("info").find({}).toArray(function(err,result){

    if(err) throw err;
    res.send(result)
    client.close();

   });
 });

});


app.listen(port, () => console.log(`Listening on port ${port}`));

标签: javascriptnode.jsheroku

解决方案


该行Error: Cannot find module 'mongodb'表明您的软件需要未安装的依赖项。要安装它,只需运行:

npm install mongodb

package.json. 您可以通过手动将依赖项添加到该dependencies部分来执行此操作:

{
  "dependencies": {
    "mongodb": "3.1.10"
  }
}

或者使用以下npm install命令:

npm install --save mongodb


推荐阅读