简体   繁体   English

Heroku node.js部署

[英]Heroku node.js deployment

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.这是我第一次使用 heroku 部署 node.js 服务器,在本地 api 很有趣,但现在似乎 heroku 无法识别 nodemon (sh:1:论坛中的一个流行问题,但我没有尝试过),它似乎是论坛中的一个流行问题,但我没有找到:评论,什么都没有。 result结果

here is my package.json:这是我的 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:这是 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:这是 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]: > backend@1.0.0 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! backend@1.0.0 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 backend@1.0.0 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

nodemon is missing from your dependencies.您的依赖项中缺少 nodemon。 Run npm install nodemon to add it to your package.json运行npm install nodemon将其添加到您的 package.json

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,需要使用npm install nodemon -D添加(-D 是将其保存到开发依赖项)

Nodemon is usually used for development environments not deployed apps. Nodemon 通常用于未部署应用程序的开发环境。

it looks like you are starting your server in index.js not server.js看起来您是在 index.js 而不是 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. npm run start:dev这将使用 nodemon 并在保存更改时刷新服务器。
  2. npm start This will run the server (useful for production) npm start这将运行服务器(对生产有用)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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