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

问题描述 投票:0回答:5

我设置了一个 JSON 文件用作小练习数据库,但我无法运行服务器。

我已经尝试在全局和本地安装(并重新安装)json-server

npm install -g json-server
npm install json-server
,然后运行
json-server --watch db.json
,但它不起作用。

我还尝试在 package.json 文件中设置一个脚本

"load": "json-server --watch db.json"
并运行该脚本
node load

但似乎没有任何效果,我不断收到回复消息:

“找不到‘json-server’命令”

或者用葡萄牙语:

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

我该如何解决这个问题?

node.js json npm npm-install json-server
5个回答
27
投票

尝试:

npx json-server --watch db.json

4
投票

解决方案: 对于 Linux:

  1. 首先,运行此命令全局安装json-server

    sudo npm install -g json-server

  2. 移动到本地文件夹(如 my-app)并运行此命令

    npm install json-server

  3. 在同一文件夹中打开新终端

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


1
投票

检查这是否对您有帮助。

打开 git bash(假设您在本地计算机上)并执行以下命令: alias json-server="<..../node_modules/.bin/json-server.cmd>" ---> 忽略 <, > 符号和 .... 是您的 node_modules 文件夹所在的位置

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


1
投票

我在我的

package.json
文件中添加了用于运行 json-server 的脚本,如下所示:

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

并尝试像这样运行它:

npm run server

它可以工作,除非服务器没有被监视,除非每次进行更改时我都必须手动结束并运行 json 服务器命令。

为我解决的问题是直接在 bash 终端(在应用程序根目录下)运行命令。

所以运行:

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

0
投票

首先你可以安装 JSON-server global... 要在项目终端中执行此操作,您可以运行

npm i -g json-server

之后,您可以使用

npm i json-server

为您的项目安装 json 服务器

下一步您可以使用

json-server --watch db.json --port 8000

运行服务器

所以在这里你可以更改端口,包括你可以运行 json 数据库的端口。

© www.soinside.com 2019 - 2024. All rights reserved.