无法将json-server识别为内部或外部命令

Abd*_*med 1 npm reactjs json-server

我正在设置一个json服务器并安装它,并将其分配为我的npm start命令,如下所示

"scripts": {
    "start": "json-server -p 3001 -w db.json"
Run Code Online (Sandbox Code Playgroud)

但是每次我在终端上键入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

Run Code Online (Sandbox Code Playgroud)

小智 7

首先,您需要检查是否json-server已全局安装。或者您可以通过以下方式全局安装

npm install -g json-server
Run Code Online (Sandbox Code Playgroud)

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

npx json-server --watch db.json
Run Code Online (Sandbox Code Playgroud)

npx和npm之间的结帐差异在这里:https ://stackoverflow.com/a/52018825/11285186


小智 7

npm i -g json-server 

json-server --watch db.json
Run Code Online (Sandbox Code Playgroud)

如果这不起作用

npm i -g json-server 

npx json-server --watch db.json
Run Code Online (Sandbox Code Playgroud)

这种方法适用于Mac


小智 6

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

就我而言,我使用的是 React js,而我的 server.json 位于我的应用程序的根目录。 在此处输入图片说明

在此处输入图片说明