首页 > 解决方案 > 如何修复'npm ERR!退出状态 64 ' npm 中的错误

问题描述

我正在设置本地服务器,并且正在运行我的服务器,npm run json:server但出现以下错误:

npm 错误!在 jsonserver@1.0.0 json:server 脚本“json-server --watch db.json”处失败。

不知道如何解决这个问题?

我试图更新npm update -g它并没有帮助。还尝试使用 没有帮助npm i -g npm 在本地安装 json 服务器npm i --save-dev json-server


    Lenovo-ideapad-990-95IKB:~/Desktop/jsonserver$ **npm run json:server**

    > jsonserver@1.0.0 json:server /home/zack/Desktop/jsonserver
    > **json-server --watch db.json**

    Could not find an option or flag "-c".

    Usage: pub <command> [arguments]

    Global options:
    -h, --help             Print this usage information.
        --version          Print pub version.
        --[no-]trace       Print debugging information when an error occurs.
        --verbosity        Control output verbosity.

              [all]        Show all output including internal tracing messages.
              [error]      Show only errors.
              [io]         Also show IO operations.
              [normal]     Show errors, warnings, and user messages.
              [solver]     Show steps during version resolution.
              [warning]    Show only errors and warnings.

    -v, --verbose          Shortcut for "--verbosity=all".

    Available commands:
      cache       Work with the system cache.
      deps        Print package dependencies.
      downgrade   Downgrade the current package's dependencies to oldest versions.
      get         Get the current package's dependencies.
      global      Work with global packages.
      help        Display help information for pub.
      publish     Publish the current package to pub.dartlang.org.
      run         Run an executable from a package.
      upgrade     Upgrade the current package's dependencies to latest versions.
      uploader    Manage uploaders for a package on pub.dartlang.org.
      version     Print pub version.

    Run "pub help <command>" for more information about a command.
    See http://dartlang.org/tools/pub for detailed documentation.

    npm ERR! Linux 4.19.5-041905-generic
    npm ERR! argv "/usr/bin/node" "/usr/bin/npm" "run" "json:server"
    npm ERR! node v8.10.0
    npm ERR! npm  v3.5.2
    npm ERR! code ELIFECYCLE
    npm ERR! jsonserver@1.0.0 json:server: `json-server --watch db.json`
    npm ERR! **Exit status 64**
    npm ERR!
    npm ERR! **Failed at the jsonserver@1.0.0 json:server script 'json-server --watch db.json'.**
    npm ERR! Make sure you have the latest version of node.js and npm installed.
    npm ERR! If you do, this is most likely a problem with the jsonserver package,
    npm ERR! not with npm itself.
    npm ERR! Tell the author that this fails on your system:
    npm ERR!     json-server --watch db.json
    npm ERR! You can get information on how to open an issue for this project with:
    npm ERR!     npm bugs jsonserver
    npm ERR! Or if that isn't available, you can get their info via:
    npm ERR!     npm owner ls jsonserver
    npm ERR! There is likely additional logging output above.

    npm ERR! Please include the following file with any support request:
    npm ERR!     /home/zack/Desktop/jsonserver/npm-debug.log```

这是 package.json:

 {
  "name": "jsonserver",
  "version": "1.0.0",
  "description": "REST API Tracker",
  "main": "index.js",
  "scripts": {
    "json:server": "json-server --watch db.json"
  },
  "author": "",
  "license": "ISC",
  "dependencies": {
    "json-server": "^0.14.2"
  }
}

预计在 localhost:3000 上启动服务器

谢谢你的帮助!

标签: node.jsnpmserverjson-server

解决方案


我认为问题在于您正在将 npm run 脚本的想法与启动json-server合并,并且还没有完全实现这两个方面。以下是我会尝试的步骤,假设您希望有一个运行脚本来启动您的 json-server

  1. 重命名db.json为,package.json因为这实际上是您package.json的节点项目的文件。您可能在运行时创建了此文件npm init。当您使用自定义脚本命令启动服务器时,此文件不是您为 json-server 模拟的 json 数据。
  2. 创建一个名为db.json的新文件,并为其提供您希望用于 json-server 的模拟 json。例如,从文档中:

db.json:

 {
      "posts": [
        { "id": 1, "title": "json-server", "author": "typicode" }
      ],
      "comments": [
        { "id": 1, "body": "some comment", "postId": 1 }
      ],
      "profile": { "name": "typicode" }
    }
  1. 现在将您的自定义运行脚本命令更改为不包含特殊字符。例如,在您的package.json中更改以下内容:

    "scripts": { "json:server": "json-server --watch db.json" },

至:

"scripts": {
    "start": "json-server --watch db.json"
  },
  1. 现在使用您的自定义脚本命令启动您的服务器npm run start。此时启动服务器应该没有任何错误。

  2. 现在如果你去http://localhost:3000/posts/1,你应该得到以下 json 响应:

{ "id": 1, "title": "json-server", "author": "typicode" }


推荐阅读