Home > Mobile >  Heroku node.js deployment
Heroku node.js deployment

Time:08-25

This is my first time deploying a node.js server with heroku, apis fun fine on local, but now it seems like heroku wont recognise nodemon (sh: 1: nodemon: not found), it seemed like a popular problem in forums but i tried every comment and nothing .result

here is my package.json:

  {
  "name": "backend",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "type": "module",
  "scripts": {
    "start":"nodemon server",
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "dependencies": {
    "cors": "^2.8.5",
    "dotenv": "^16.0.1",
    "express": "^4.18.1",
    "mongodb": "^4.8.0"
  },
  "engines":{
    "node":"14.x"
  }
}

this is the index.js:

import app from "./server.js"
import mongodb from "mongodb"
import dotenv from "dotenv"
import jobsDAO from "./dao/jobsDAO.js"
dotenv.config()
const MongoClient = mongodb.MongoClient

const port = process.env.PORT || 5000

MongoClient.connect(
    process.env.JOBSFINDER_DB_URI,
    {
        wtimeoutMS: 2500,
    }
)
.catch(err => {
    console.error(err.stack)
    process.exit(1)
})
.then(async client =>{
    await jobsDAO.injectDB(client)
    app.listen(port, () => {
        console.log("listening on port",port)
    })
})

this is the server.js:

import express from "express"
import cors from "cors"
import jobs from "./api/jobs.route.js"

const app = express()

app.use(cors())
app.use(express.json())

app.use("/api/v1/jobs", jobs)
app.use("*",(req,res)=>res.status(404).json({ error: "Not Found"}))

export default app 
2022-08-24T20:58:55.624084 00:00 heroku[web.1]: Starting process with command `npm start`
2022-08-24T20:58:57.160097 00:00 app[web.1]:
2022-08-24T20:58:57.160109 00:00 app[web.1]: > [email protected] start /app
2022-08-24T20:58:57.160109 00:00 app[web.1]: > nodemon server
2022-08-24T20:58:57.160109 00:00 app[web.1]:
2022-08-24T20:58:57.170277 00:00 app[web.1]: sh: 1: nodemon: not found
2022-08-24T20:58:57.174407 00:00 app[web.1]: npm ERR! code ELIFECYCLE
2022-08-24T20:58:57.174655 00:00 app[web.1]: npm ERR! syscall spawn
2022-08-24T20:58:57.174717 00:00 app[web.1]: npm ERR! file sh
2022-08-24T20:58:57.174792 00:00 app[web.1]: npm ERR! errno ENOENT
2022-08-24T20:58:57.177522 00:00 app[web.1]: npm ERR! [email protected] start: `nodemon server`
2022-08-24T20:58:57.177629 00:00 app[web.1]: npm ERR! spawn ENOENT
2022-08-24T20:58:57.177682 00:00 app[web.1]: npm ERR!
2022-08-24T20:58:57.177719 00:00 app[web.1]: npm ERR! Failed at the [email protected] start script.
2022-08-24T20:58:57.177754 00:00 app[web.1]: npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
2022-08-24T20:58:57.182613 00:00 app[web.1]:
2022-08-24T20:58:57.182689 00:00 app[web.1]: npm ERR! A complete log of this run can be found in:
2022-08-24T20:58:57.182729 00:00 app[web.1]: npm ERR!     /app/.npm/_logs/2022-08-24T20_58_57_178Z-debug.log
2022-08-24T20:58:57.299986 00:00 heroku[web.1]: Process exited with status 1

CodePudding user response:

nodemon is missing from your dependencies. Run npm install nodemon to add it to your package.json

CodePudding user response:

nodemon is missing from you dependencies and needs to be added using npm install nodemon -D (the -D is to save it to dev dependencies)

Nodemon is usually used for development environments not deployed apps.

it looks like you are starting your server in index.js not server.js

I would suggest the following

  1. npm install nodemon -D

  2. change your scripts to:

    "scripts": { "start":"node index.js", "start:dev": "nodemon index.js" "test": "echo \"Error: no test specified\" && exit 1" },

To run the app locally you can use:

  1. npm run start:dev This will use nodemon and refresh the server when changes are saved.
  2. npm start This will run the server (useful for production)
  • Related