簡體   English   中英

nodemon 應用程序在啟動前等待文件更改崩潰

[英]nodemon app crashed waiting for file changes before starting

$ npm啟動

server@1.0.0 啟動 nodemon index.js

[nodemon] 2.0.19 [nodemon] 隨時重啟,輸入rs [nodemon] watch path(s): [nodemon] 觀看擴展:js,mjs,JSON [nodemon] 啟動node index.js D:\React\authInMern\server\index.js:8 connection(); ^

類型錯誤:連接不是 Object 的 function。 (D:\React\authInMern\server\index.js:8:1) 在 Module._compile (node:internal/modules/cjs/loader:1126:14) 在 Object.Module._extensions..js (node:inter在 Function.Module._load (node:internal/modules/cjs/loader) 的 Module.load (node:internal/modules/cjs/loader:1004:32) 處enter code here nal/modules/cjs/loader:1180:10) :839:12) 在 Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:81:12) 在 node:internal/main/run_main_module:17:47 [nodemon] 應用程序崩潰 - 在啟動前等待文件更改...

index.js

require('dotenv').config();
const express = require("express");
const app = express();
const cors = require("cors");
const { connection } = require("./db");

// database connection
connection();

// Middlewares
app.use(express.json())
app.use(cors());

const port = process.env.PORT || 8080;`enter code here`
app.listen(port, () => console.log(`Listening on port $(port)...`)); 
db.js

const mongoose = require("mongoose");

module.exports = () => {
    const connectionParams = {
        useNewUrlParser: true,
        useUnifiedTopology: true,
    };
    try {
       mongoose.connect(process.env.DB, connectionParams);
       console.log("Connected to database successfully")
    } catch (error) {
        console.log(error);
        console.log("could not connect to database");
        
    }
};

這樣做,如果你想連接 function 被解構

const mongoose = require("mongoose");

const connection = () => {
    const connectionParams = {
        useNewUrlParser: true,
        useUnifiedTopology: true,
    };
    try {
       mongoose.connect(process.env.DB, connectionParams);
       console.log("Connected to database successfully")
    } catch (error) {
        console.log(error);
        console.log("could not connect to database");
        
    }
};

module.exports = { connection }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM