【问题标题】:Nodemon Server won't restartNodemon 服务器不会重新启动
【发布时间】:2026-02-14 15:10:02
【问题描述】:

如果文件有任何更改,Nodemon 不会每次都重新启动,我需要使用 Ctrl-C 停止服务器并重新启动服务器。我在 Windows 64bit 上,我在第 30 行进行了更改,但服务器没有重新启动。为什么不重启?

const express = require('express');
const bodyparser =require('body-parser')

const app = express();
app.use(bodyparser.json());


const database = {
    users:[
        {
            id:'123',
            name:'john',
            email:'john@gmail.com',
            password:'cookies',
            entries:0,
            joined: new Date()
        },
        {
            id:'124',
            name:'sally',
            email:'sally@gmail.com',
            password:'bananas',
            entries:0,
            joined: new Date()
        }
    ]
}

app.get('/',(req,res)=>{
    res.json(database.users);
})


app.post('/signin',(req,res)=>{
    if(req.body.email === database.users[0].email && req.body.password === database.users[0].password){
        res.json('success');
    }
    else{
        res.status(400).json('error logging in');
    }

})


app.post('/register',(req,res)=>{
    const {name , email , password} = req.body;
    database.users.push({
            id:'125',
            name:name,
            email:email,
            password:password,
            entries:0,
            joined: new Date()

    })
    res.json(database.users[database.users.length-1])
})


app.listen(3000,()=>{
    console.log("listening to port 3000");
})

这是我的 package.json

{
  "name": "Backend-brain-api",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "start": "nodemon server.js"
  },
  "dependencies": {
    "body-parser": "^1.19.0",
    "express": "^4.17.1",
    "ts-node": "^8.8.1"
  },
  "devDependencies": {
    "nodemon": "^2.0.2"
  },
  "keywords": [],
  "author": "",
  "license": "ISC"
}

如果我在 package.json 中定义一个启动脚本,它会给我一个全新的错误:

> Backend-brain-api@1.0.0 start D:\Nitin\Node.js & Express.js\Backend-brain-api
> nodemon server.js

'Express.js\Backend-brain-api\node_modules\.bin\' is not recognized as an internal or external command,
operable program or batch file.
internal/modules/cjs/loader.js:985
  throw err;
  ^

Error: Cannot find module 'D:\Nitin\nodemon\bin\nodemon.js'
    at Function.Module._resolveFilename (internal/modules/cjs/loader.js:982:15)
    at Function.Module._load (internal/modules/cjs/loader.js:864:27)
    at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:74:12)
    at internal/main/run_main_module.js:18:47 {
  code: 'MODULE_NOT_FOUND',
  requireStack: []
}
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! Backend-brain-api@1.0.0 start: `nodemon server.js`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the Backend-brain-api@1.0.0 start script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

【问题讨论】:

  • 无法重现(尽管在 macOS 上) - 如果我将您的 package.jsonserver.js 复制到新目录,运行 npm i && npm start,然后编辑 server.js 我看到 nodemon 将其重新启动为预计。

标签: javascript node.js express visual-studio-code nodemon


【解决方案1】:

不要在终端中输入 nodemon server.js,而是输入 npm start。这应该在你的 package.json 中执行启动脚本,从而在 server.js 上运行 nodemon。

希望对你有帮助

【讨论】:

  • 我正在运行npm start 仍然没有工作
  • 在您的终端中,您与您的 package.json 位于同一目录中?
  • 是的,我在同一目录中运行终端
  • 你能传递一个 github repo 的链接吗?
  • 站外代码链接只能作为补充,minimal reproducible example 必须在问题中。鼓励编辑,而不是链接。
【解决方案2】:

这是一个 Windows 路径的东西。 Windows 无法获取路径,因为它包含不必要的特殊字符,例如 & 和路径名中的空格。看起来 Node 无法解析路径。

【讨论】:

    最近更新 更多