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

如何在package.json的scripts中配置本地启动MongoDB与Nodemon?

Automate MongoDB + Nodemon Startup with npm Scripts

Absolutely! You can ditch the copy-pasting from text files by setting up custom npm scripts in your package.json—this will let you start both MongoDB and Nodemon with a single command. Let’s break this down into easy steps:

Step 1: Install a tool to run multiple commands concurrently

Since we need to start both MongoDB and Nodemon at the same time, we’ll use concurrently (a popular package for running multiple scripts in parallel). Install it as a dev dependency:

npm install --save-dev concurrently

Step 2: Configure your package.json scripts

Open your package.json file and add the following to the "scripts" section. Note that the MongoDB command might vary slightly based on your operating system:

For Mac/Linux:

"scripts": {
  // Start MongoDB (adjust the --dbpath if your data directory is elsewhere)
  "start:mongo": "mongod --dbpath ./data/db",
  // Start Nodemon (replace "server.js" with your actual entry file)
  "start:server": "nodemon server.js",
  // Run both scripts at once
  "dev": "concurrently \"npm run start:mongo\" \"npm run start:server\""
}

For Windows:

The only difference is the file path syntax for the MongoDB data directory:

"scripts": {
  "start:mongo": "mongod --dbpath .\\data\\db",
  "start:server": "nodemon server.js",
  "dev": "concurrently \"npm run start:mongo\" \"npm run start:server\""
}

Key Notes:

  • MongoDB Data Directory: If you don’t have a data/db folder in your project root, create it first:
    • Mac/Linux: mkdir -p data/db
    • Windows: mkdir data\db
  • MongoDB as a System Service: If you installed MongoDB as a system service (common on Windows), replace the start:mongo command with net start MongoDB (to launch the service) or just mongod if it’s already in your system PATH.
  • Entry File: Make sure to replace "server.js" with your actual server entry file (like app.js or index.js).

Step 3: Run your automated workflow

Now you can start both services with a single terminal command:

npm run dev

Concurrently will handle running both MongoDB and Nodemon in the same terminal, showing output from both processes. If you need to stop them, just press Ctrl + C once to terminate both scripts.

Troubleshooting Common Hiccups

  • MongoDB won’t start: Double-check the --dbpath path—make sure the folder exists and MongoDB has permission to access it. If you get a "command not found" error, ensure MongoDB is added to your system PATH.
  • Nodemon can’t connect to MongoDB: Sometimes MongoDB takes a second to finish initializing. If you run into connection errors, install the wait-on package (npm install --save-dev wait-on) to delay Nodemon until MongoDB is ready, then update your dev script:
    "dev": "npm run start:mongo & wait-on mongodb://localhost:27017 && npm run start:server"
    
    (This syntax works best on Mac/Linux; for Windows, you might need to adjust to use batch file logic or a cross-platform alternative.)

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.25 03:44:17