简体   繁体   English

命令 json-server --watch db.json 不工作 - 它返回“'json-server' 命令未找到”

[英]Command json-server --watch db.json is not working - it returns"'json-server' command not found"

I set a JSON file to use as a little practice database, but I can't run the server.我设置了一个 JSON 文件作为小练习数据库,但是我无法运行服务器。

I've already tried to install (and reinstall) json-server global and locally npm install -g json-server and npm install json-server and then run json-server --watch db.json , but it doesn't work.我已经尝试在全球和本地安装(并重新安装)json-server npm install -g json-servernpm install json-server然后运行json-server --watch db.json ,但它不起作用。

I've also tried to set a script in the package.json file "load": "json-server --watch db.json" and run the script node load .我还尝试在 package.json 文件中设置脚本"load": "json-server --watch db.json"并运行脚本node load

But nothing seems to work and I keep getting in return the message:但似乎没有任何效果,我不断收到回复消息:

"'json-server' command not found" “找不到‘json-server’命令”

or, in Portuguese:或者,用葡萄牙语:

"'json-server' não é reconhecido como um comando interno ou externo, um programa operável ou um arquivo em lotes". “'json-server' não é reconhecido como um comando interno ou externo,um programa operável ou um arquivo em lotes”。

How can I fix this?我怎样才能解决这个问题?

尝试:

npx json-server --watch db.json

Check if this might help you.检查这是否可以帮助您。

Open the git bash(assume you have on your local machine) and execute below command: alias json-server="<..../node_modules/.bin/json-server.cmd>" ---> IGNORE <, > symbols and .... is the location where your node_modules folder is present打开 git bash(假设您在本地机器上有)并执行以下命令: alias json-server="<..../node_modules/.bin/json-server.cmd>" ---> IGNORE <, >符号和 .... 是 node_modules 文件夹所在的位置

Then run json-server --watch db.json然后运行json-server --watch db.json

Solution : For Linux:解决方案:对于 Linux:

  1. Firstly, run this command to globally install json-server首先,运行此命令全局安装json-server

    sudo npm install -g json-server

  2. Move to your local folder(like my-app) and run this command移动到您的本地文件夹(如 my-app)并运行此命令

    npm install json-server

  3. Open a new Terminal in the same folder在同一文件夹中打开一个新终端

    json-server --watch db.json --port 3004

I added the script for running the json-server in my package.json file like this:我在我的package.json文件中添加了用于运行 json-server 的脚本,如下所示:

 "server": "json-server db.json --watch --port 5000"

And tried to run it like this:并尝试像这样运行它:

npm run server

And it works except the server wasn't being watched unless I had to manually end and run the json server command every time a change was made.它可以正常工作,除非服务器没有被监视,除非每次进行更改时我都必须手动结束并运行 json 服务器命令。

What fixed it for me was running the command directly in a bash terminal (under the app root directory).为我修复它的是直接在 bash 终端(在应用程序根目录下)中运行命令。

So run:所以运行:

json-server db.json --watch --port 5000

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM