You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Nodemon无法正常工作:执行nodemon启动index.js时服务未加载

排查步骤及解决方法

1. 先确认nodemon的调用方式正确

你执行npm i -D nodemon是将nodemon安装为项目局部开发依赖,默认情况下终端无法直接识别nodemon全局命令,有三种解决方式可选:

  • 全局安装nodemon:执行npm i -g nodemon后,再运行nodemon index.js即可
  • 用npx调用局部安装的nodemon:直接执行npx nodemon index.js
  • 在项目package.json中配置启动脚本:
    在scripts字段中添加如下配置后,执行npm run dev即可启动
    "scripts": {
      "dev": "nodemon index.js"
    }
    

2. 修复mongoose连接代码的兼容性问题

你当前使用的mongoose回调式连接方法在Mongoose 6.0及以上版本已经被废弃,会直接抛出异常导致服务启动失败,将db.js替换为如下代码即可:

const mongoose = require('mongoose');
const mongoURI = "mongodb://localhost:27017/?readPreference=primary&appname=MongoDB%20Compass&ssl=false";

const connectToMongo = async ()=>{
    try {
        await mongoose.connect(mongoURI);
        console.log('database connected');
    } catch (err) {
        console.error('数据库连接失败:', err.message);
    }
}

module.exports = connectToMongo;

3. 排查本地环境配置问题

  • 确认本地MongoDB服务已经正常启动,27017端口未被禁用或占用
  • 确认3000端口未被其他应用占用,可将index.js中的port值改为3001等其他未使用端口测试
  • 先直接执行node index.js启动服务,查看终端输出的报错信息,先确保代码本身可以正常运行,再排查nodemon相关问题

内容的提问来源于stack exchange,提问作者yashrith

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.24 21:45:04