如何修复npm ERR! npm中退出状态为64'的错误

时间:2019-02-04 13:41:25

标签: node.js npm server json-server

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

  

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

不知道该如何解决?

我尝试更新npm update -g,但没有帮助。 也尝试过npm i -g npm 并使用npm i --save-dev json-server在本地安装json服务器 无济于事


    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上启动服务器

谢谢您的帮助!

1 个答案:

答案 0 :(得分:1)

我认为问题是您正在将npm运行脚本的思想与启动json-server合并,并且尚未完全实现任何一个方面。以下是我尝试的步骤,假设您希望有一个运行脚本来启动json服务器

  1. db.json重命名为package.json ,因为这实际上是您的节点项目的package.json文件。您可能在运行npm init时创建了此文件。当您使用自定义脚本命令启动服务器时,该文件不是您要为json服务器模拟的json数据。
  2. 创建一个名为 db.json 的新文件,并将要用于json服务器的模拟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" }