简体   繁体   中英

8Cant deploy Nodejs server in Heroku : 'Error: Cannot find module 'getStream''

My project seems to have no issues when running locally, but when I try to deploy it in heroku and open the app it gives me an application error. I tried checking the logs in heroku and this is what I get.

2022-09-14T20:31:10.475090+00:00 app[web.1]: at Module.require (node:internal/modules/cjs/loader:1028:19)
2022-09-14T20:31:10.475091+00:00 app[web.1]: at require (node:internal/modules/cjs/helpers:102:18)
2022-09-14T20:31:10.475091+00:00 app[web.1]: at Object.<anonymous> (/app/controllers/auth.js:1:19) 
2022-09-14T20:31:10.475092+00:00 app[web.1]: at Module._compile (node:internal/modules/cjs/loader:1126:14)
2022-09-14T20:31:10.475092+00:00 app[web.1]: at Object.Module._extensions..js (node:internal/modules/cjs/loader:1180:10)
2022-09-14T20:31:10.475092+00:00 app[web.1]: at Module.load (node:internal/modules/cjs/loader:1004:32)
2022-09-14T20:31:10.475093+00:00 app[web.1]: at Function.Module._load (node:internal/modules/cjs/loader:839:12)
2022-09-14T20:31:10.475093+00:00 app[web.1]: at Module.require (node:internal/modules/cjs/loader:1028:19) {
2022-09-14T20:31:10.475093+00:00 app[web.1]: code: 'MODULE_NOT_FOUND',
2022-09-14T20:31:10.475094+00:00 app[web.1]: requireStack: [
2022-09-14T20:31:10.475094+00:00 app[web.1]: '/app/controllers/auth.js',
2022-09-14T20:31:10.475094+00:00 app[web.1]: '/app/routes/auth.js',
2022-09-14T20:31:10.475095+00:00 app[web.1]: '/app/index.js'
2022-09-14T20:31:10.475095+00:00 app[web.1]: ]
2022-09-14T20:31:10.475095+00:00 app[web.1]: }
2022-09-14T20:31:10.660749+00:00 heroku[web.1]: Process exited with status 1
2022-09-14T20:31:10.723193+00:00 heroku[web.1]: State changed from starting to crashed
2022-09-14T20:31:10.726441+00:00 heroku[web.1]: State changed from crashed to starting
2022-09-14T20:31:13.020405+00:00 heroku[web.1]: Starting process with command `npm start`
2022-09-14T20:31:16.234548+00:00 app[web.1]:
2022-09-14T20:31:16.234562+00:00 app[web.1]: > server@1.0.0 start
2022-09-14T20:31:16.234562+00:00 app[web.1]: > node index.js
2022-09-14T20:31:16.234563+00:00 app[web.1]:
2022-09-14T20:31:16.387441+00:00 app[web.1]: node:internal/modules/cjs/loader:959
2022-09-14T20:31:16.387443+00:00 app[web.1]: throw err;
2022-09-14T20:31:16.387444+00:00 app[web.1]: ^
2022-09-14T20:31:16.387444+00:00 app[web.1]:
2022-09-14T20:31:16.387444+00:00 app[web.1]: Error: Cannot find module 'getStream'
2022-09-14T20:31:16.387445+00:00 app[web.1]: Require stack:
2022-09-14T20:31:16.387445+00:00 app[web.1]: - /app/controllers/auth.js
2022-09-14T20:31:16.387446+00:00 app[web.1]: - /app/routes/auth.js
2022-09-14T20:31:16.387446+00:00 app[web.1]: - /app/index.js
2022-09-14T20:31:16.387446+00:00 app[web.1]: at Function.Module._resolveFilename (node:internal/modules/cjs/loader:956:15)
2022-09-14T20:31:16.387447+00:00 app[web.1]: at Function.Module._load (node:internal/modules/cjs/loader:804:27)
2022-09-14T20:31:16.387447+00:00 app[web.1]: at Module.require (node:internal/modules/cjs/loader:1028:19)
2022-09-14T20:31:16.387448+00:00 app[web.1]: at require (node:internal/modules/cjs/helpers:102:18) 
2022-09-14T20:31:16.387448+00:00 app[web.1]: at Object.<anonymous> (/app/controllers/auth.js:1:19) 
2022-09-14T20:31:16.387448+00:00 app[web.1]: at Module._compile (node:internal/modules/cjs/loader:1126:14)
2022-09-14T20:31:16.387449+00:00 app[web.1]: at Object.Module._extensions..js (node:internal/modules/cjs/loader:1180:10)
2022-09-14T20:31:16.387449+00:00 app[web.1]: at Module.load (node:internal/modules/cjs/loader:1004:32)
2022-09-14T20:31:16.387449+00:00 app[web.1]: at Function.Module._load (node:internal/modules/cjs/loader:839:12)
2022-09-14T20:31:16.387449+00:00 app[web.1]: at Module.require (node:internal/modules/cjs/loader:1028:19) {
2022-09-14T20:31:16.387450+00:00 app[web.1]: code: 'MODULE_NOT_FOUND',
2022-09-14T20:31:16.387450+00:00 app[web.1]: requireStack: [
2022-09-14T20:31:16.387450+00:00 app[web.1]: '/app/controllers/auth.js',
2022-09-14T20:31:16.387450+00:00 app[web.1]: '/app/routes/auth.js',
2022-09-14T20:31:16.387451+00:00 app[web.1]: '/app/index.js'
2022-09-14T20:31:16.387451+00:00 app[web.1]: ]
2022-09-14T20:31:16.387451+00:00 app[web.1]: }
2022-09-14T20:31:16.528452+00:00 heroku[web.1]: Process exited with status 1
2022-09-14T20:31:16.728856+00:00 heroku[web.1]: State changed from starting to crashed
2022-09-14T20:31:55.119571+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET pad="190.237.25.187" dyno= connect= service= status=503 bytes= protocol=https2022-09-14T20:42:01.202592+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/" host=file-iu-sys.herokuapp.com request_id=b157556e-1a74-4d16-aef5-7e2e8cea2612 fwd="190.237.25.187" dyno= connect= service= status=503 bytes= protocol=https  2022-09-14T20:42:02.113418+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/favicon.ico" host=file-iu-sys.herokuapp.com request_id=2c0cf996-5b33-4d15-b7c6-88cedd9f9c25 fwd="190.237.25.187" dyno= connect= service= status=503 bytes= protocol=https

I have tried checking for missing scripts, changing the Port to process.env.PORT , heroku repo:reset , deleting the git file and starting a new git init and even deleting the node_modules folder and running npm install . A similar app I tried hosting in heroku seems to run with no problems, the difference being this one has a mysql dependency and the heroku app has a clearDB plugin. The 'getstream' version is '8.0.1' (the one that runs without problems is '8.0.0' could that be the issue?).

this is my package.json file:

{
  "name": "server",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "start": "node index.js",
    "dev": "nodemon index.js"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "dependencies": {
    "bcrypt": "^5.0.1",
    "body-parser": "^1.20.0",
    "cors": "^2.8.5",
    "crypto": "^1.0.1",
    "dotenv": "^16.0.1",
    "express": "^4.18.1",
    "getstream": "^8.0.1",
    "mysql": "^2.18.1",
    "nodemon": "^2.0.16",
    "stream-chat": "^6.5.1"
  }
}

index.js file:

const express = require('express');
const cors = require('cors');
const mysql = require('mysql');
const bodyParser = require('body-parser')

const authRoutes = require('./routes/auth.js')

const connection = mysql.createPool({
  host: '*host-name*',
  user: '*user*',
  password: '*password*',
  database: '*database*'
})

const app = express();
const PORT = process.env.PORT || 3001;

require('dotenv').config();

app.use(cors());
app.use(express.json());
app.use(express.urlencoded());
//Routes
app.use('/auth', authRoutes);

app.get('/', (req, res) =>{
  res.send('Server an')
})

//DB connection

//get from DB
app.get('/api/get', (req, res)=>{
  const sqlSelect = 'SELECT * FROM anmeldungen';
  connection.query(sqlSelect, (err, result)=>{
    res.send(result);
    if(err){
      console.log('Error fetching from db');
    }
  });
})

//write to DB
app.use(bodyParser.urlencoded({extended: true}));

app.post('/api/insert', (req, res)=>{
  const Student = req.body.Student;
  const Fach = req.body.Fach;
  //const Datum = req.body.Datum;
  const Datum = new Date().toISOString().split('T')[0];
  const DateiName = req.body.DateiName;
  const Seite = req.body.Seite;
  const Beschreibung = req.body.Beschreibung;

  const sqlInsert = 'INSERT INTO anmeldungen(Student, Fach, Datum, DateiName, Seite, Beschreibung) VALUES(?,?,?,?,?,?)'
  connection.query(sqlInsert, [Student, Fach, Datum, DateiName, Seite, Beschreibung], (err, result)=>{
    console.log(result);
  });
});

//delete from db
app.delete('/api/delete/:id', (req, res) =>{
  const rowId = req.params.id;
  const sqlDelete = "DELETE FROM anmeldungen WHERE id = ?";
  connection.query(sqlDelete, rowId, (err, result)=>{
    if(err) console.log(err);
  })
})

//update rows in db
app.put('/api/update/:id', (req, res) =>{
  const rowId = req.params.id;
  const rowStat = req.body.Status;
  const sqlUpdate = "UPDATE anmeldungen SET Status = ? WHERE id = ?";
  connection.query(sqlUpdate, [rowStat, rowId], (err, result)=>{
    if(err) console.log(err);
  })
})

app.listen(PORT, () => console.log(`Server lauft auf port ${PORT}`));

auth.js:

const {connect} = require('getStream');
const bcrypt = require('bcrypt');
const StreamChat = require('stream-chat').StreamChat;
const crypto = require('crypto');

require('dotenv').config();

const api_key = process.env.STREAM_API_KEY;
const api_secret = process.env.STREAM_API_SECRET;
const app_id = process.env.STREAM_APP_ID;

const signup = async (req, res) => {
  try {
    //get from the front-end
    const {fullName, username, password, phoneNumber} = req.body;
    //create a random user Id: 16 digits in hexadecimal
    const userId = crypto.randomBytes(16).toString('hex');
    //connection to stream
    const serverClient = connect(api_key, api_secret, app_id);
    //create a password for the user token 
    const hashedPassword = await bcrypt.hash(password, 10);
    //create token for the user
    const token = serverClient.createUserToken(userId);
    
    //get the values straight from the front-end to ensure secure authentication
    res.status(200).json({token, fullName, username, userId, hashedPassword, phoneNumber})

  } catch (error) {
    console.log(error);
    res.status(500).json({message: error});
  }
};

const login = async(req, res) => {
  try {
    const {username, password} = req.body;
    const serverClient = connect(api_key, api_secret, app_id);
    //new instance of streamChat
    const client = StreamChat.getInstance(api_key, api_secret);
    //query all users that match this username
    const {users} = await client.queryUsers({name: username});

    if(!users.length) return res.status(400).json({message: 'Benutzername nicht gefunden.'});

    //decrypt the password to see if it matches
    const success = await bcrypt.compare(password, users[0].hashedPassword);
    //create new token for this specific user's id.
    const token = serverClient.createUserToken(users[0].id);
    if(success){
      res.status(200).json({token, fullName: users[0].fullName, username, userId: users[0].id});
    } else {
      res.status(500).json({message: 'falsches Kennwort'});
    }

  } catch (error) {
    console.log(error);
    res.status(500).json({message: error});
  }
};

module.exports = {signup, login}

Found the problem in the auth.js file: I had written const {connect} = require('getStream'); instead of const {connect} = require('getstream'); . So I guess it was due to naming conventions in a way. @Chris ty, that first question was right on the money.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM