json-server未被识别为内部或外部命令。

38

我正在设置一个json-server,我已经安装它并将其分配为我的npm启动命令,如下所示

我正在配置一个json-server,并已安装并将其指定为我的npm启动命令,如下所示

"scripts": {
    "start": "json-server -p 3001 -w db.json"
但每次我在终端上输入“npm start”时,都会出现此错误。
> api-server@1.0.0 start C:\Work\React-projects\streams\api-server
> json-server -p 3001 -w db.json

'json-server' is not recognized as an internal or external command,
operable program or batch file.
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! api-server@1.0.0 start: `json-server -p 3001 -w db.json`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the api-server@1.0.0 start script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

npm ERR! A complete log of this run can be found in:
npm ERR!     C:\Users\Abdelaziz\AppData\Roaming\npm-cache\_logs\2019-04-06T09_07_30_796Z-debug.log


你是如何安装json-server的? - Arnaud Christ
@ArnaudChrist 我尝试了一切 npm install -g json-server --save - Abdelaziz Mohamed
2
对于访问者来说,您可以直接使用 NPX 来查看 JSON 文件,甚至无需安装 JSON 服务器,像这样 "npx json-server --watch ./data/filename.json"。 - Sagive
7个回答

134

首先,您需要检查全局安装了json-server,如果没有安装,您可以通过以下方式进行全局安装:

First, you need to check json-server installed globally or not. or you can install it globally by


npm install -g json-server

如果您在项目中本地安装它,请使用 npx 来运行它。

npx json-server --watch db.json

在这里查看npx和npm之间的区别:https://dev59.com/8VUL5IYBdhLWcg3wQWKD#52018825


我全局安装了它,但仍然无法工作。我尝试使用 npx,它显示如下信息: ( npx: 在 8.971 秒内安装了 236 个软件包 找不到命令:json-server ) - Abdelaziz Mohamed
我得到了这个:C:\Users\Abdelaziz\AppData\Roaming\npm +-- create-react-app@2.1.8 +-- gulp-cli@2.1.0 +-- http-server@0.11.1 +-- json-server@0.14.2 +-- npm@6.9.0 `-- sudo@1.0.3 - Abdelaziz Mohamed
1
所以,下一步是检查您的PATH环境变量,确保它包含C:\Users\Abdelaziz\AppData\Roaming\npm - namth
尝试运行以下命令:node "C:\Users\Abdelaziz\AppData\Roaming\npm\node_modules\json-server\bin\index.js" -p 3001 -w db.json - namth
将PATH环境变量添加进去,问题就解决了。 - Donki
显示剩余4条评论

14

第一步,您需要使用以下命令安装json server:
npm install -g json-server
然后,您可以运行以下命令并选择端口:
npx json-server --watch -p 3333 server.json
3333更改为您想要的端口号,并将server.json替换为您虚拟API的名称。

在我的情况下,我正在使用React js,我的server.json位于应用程序的根目录中。 enter image description here

enter image description here


7
npm i -g json-server 

json-server --watch db.json

如果这不起作用

npm i -g json-server 

npx json-server --watch db.json

这种方法适用于 Mac。


3
当我尝试将json-server添加为package.json文件中的脚本以便用npm run start运行时,遇到了同样的问题。错误提示"json-server is not recognized as an internal or external command"。解决方法是修改scripts部分如下:"scripts": { "start": "npx json-server -p 3001 -w db.json" } 或者 "scripts": { "server" : "npx json-server --watch db.json --port 3001" }

3

更新Node.js版本,适用于Node.js 10及以上版本。


1

您可以尝试:

  1. npm i -g json-server
  2. 进入您的应用程序路径,例如:\angular\TestApps\src\assets\mockdata>
  3. 运行 npx json-server --watch .\db.json 例如:这里的 db.josn 是我的虚拟 Web API
  4. 这将在 http://localhost:3000/ 上无问题地运行 json server

0

常见错误是没有全局安装json-server包

对我有用的是我使用了以下命令:

npm install -g json-server

(-g用于全局安装包)

用于测试:

json-server --watch data/db.json --port 8000 (--watch用于检查数据库文件[此处路径-data data/db.json],--port用于手动设置端口[此处为8000])

现在您可以在端口8000上检查数据了,

http://localhost:8000/anything

(您也可以使用npx)


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接