【问题标题】:json-server is not recognized as an internal or external commandjson-server 未被识别为内部或外部命令
【发布时间】:2019-08-28 01:50:25
【问题描述】:

我正在设置一个 json-server 并安装它并将其分配给我的 npm start 命令,就像这样

"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 的?
  • @ArnaudChrist 我什么都试过了``` npm install -g json-server --save ```
  • 对于访问的人..您可以直接使用 NPX 观看 json 文件,甚至无需安装 json-server 像这样 "npx json-server --watch ./data/filename.json"

标签: reactjs npm json-server


【解决方案1】:

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

全局安装它
npm install -g json-server

如果你在你的项目本地安装它,使用npx运行它

npx json-server --watch db.json

在这里查看 npx 和 npm 的区别:https://stackoverflow.com/a/52018825/11285186

【讨论】:

  • 我在全局安装了它仍然无法正常工作,我尝试了 npx 它显示了这个( npx: installed 236 in 8.971s command not found: json-server )
  • 您好,您可以运行npm list -g --depth 0并在列表中检查json-server吗?
  • 我得到了这个: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
  • 所以,下一步是检查你的 PATH 环境,确保它有 C:\Users\Abdelaziz\AppData\Roaming\npm
  • 做了几次,还是一样的错误
【解决方案2】:

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

就我而言,我使用的是 React js,而我的 server.json 位于我的应用程序的根目录中。

【讨论】:

    【解决方案3】:
    npm i -g json-server 
    
    json-server --watch db.json
    

    如果这不起作用

    npm i -g json-server 
    
    npx json-server --watch db.json
    

    这种方式适用于mac

    【讨论】:

      【解决方案4】:

      更新 node.js 版本,适用于 node.js 10 版本以上的版本。

      【讨论】:

        【解决方案5】:

        你可以试试:

        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 服务器,没有任何问题

        【讨论】:

          【解决方案6】:

          当我尝试将 json-server 作为脚本添加到 package.json 文件中以便使用它运行时,我遇到了同样的问题

          npm run start

          收到错误“json-server 未被识别为内部或外部命令”

          一个快速的解决方案是将脚本修改为

          "scripts": { "start": "npx json-server -p 3001 -w db.json" }

          另一种方法是

          "scripts": { "server" : "npx json-server --watch db.json --port 3001" }

          【讨论】:

            【解决方案7】:

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

            什么对我有用:我使用了命令:

            npm install -g json-server

            (-g 用于全局安装包)

            用于测试:

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

            你现在可以查看8000端口的数据了,

            http://localhost:8000/任何东西

            (你也可以使用npx)

            【讨论】:

              猜你喜欢
              • 2014-08-06
              • 2019-01-31
              • 1970-01-01
              • 2014-04-29
              • 1970-01-01
              • 1970-01-01
              • 2013-10-06
              • 2013-10-11
              • 2013-01-25
              相关资源
              最近更新 更多