Windows系统中json-server命令未被识别的问题求助
Hey, I’ve dealt with this exact frustration before when working with npm global packages on Windows—let’s walk through the fixes step by step:
1. First, make sure json-server is installed globally
If you skipped the -g flag when installing, json-server only lives in your project’s local node_modules folder and won’t be available system-wide. Let’s fix that first:
npm install -g json-server
To confirm it installed correctly, run:
npm list -g json-server
You should see a path pointing to the global installation.
2. Check if npm’s global bin path is in your system PATH
Windows needs to know where to look for globally installed npm commands. Here’s how to verify:
- Open Command Prompt and run this to get npm’s global prefix path:
You’ll usually get something likenpm config get prefixC:\Users\YourUsername\AppData\Roaming\npm(replaceYourUsernamewith your actual Windows username). - Navigate to that folder in File Explorer—you should see
json-server.cmdthere. If it exists, the problem is just that this path isn’t in your system’s PATH variable.
3. Add the npm global path to your system PATH
This is the permanent fix:
- Right-click This PC → select Properties → click Advanced system settings → in the pop-up, go to Environment Variables.
- In the System variables section, find the
Pathvariable and click Edit. - Click New, paste the path you got from
npm config get prefix, then click OK on all open windows to save changes. - Critical step: Close all open Command Prompt windows and open a new one—environment variable changes don’t take effect in already open terminals.
4. Quick temporary workaround (not ideal for long-term use)
If you need to use json-server right now without modifying PATH, you can run it directly using its full path:
C:\Users\YourUsername\AppData\Roaming\npm\json-server.cmd --watch db.json
Just swap in your actual username and your data file name (like db.json).
内容的提问来源于stack exchange,提问作者The Dark Knight

