最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Unexpected errors and warnings when running json-server --watch - Stack Overflow

programmeradmin4浏览0评论

I'm trying to use json-server as follows:

$ json-server --watch db.json

However, I'm getting errors or warnings when I run that mand, depending on the version I have installed:

  • 1.0.0-alpha.1-1.0.0-alpha.12:

    sh: json-server: mand not found
    

    or (on Windows):

    The term 'json-server' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
    At line:1 char:1
    + json-server --watch db.json
    

    or (if executed via npx):

    npm ERR! could not determine executable to run
    
  • 1.0.0-alpha.13:

    node:internal/errors:496
        ErrorCaptureStackTrace(err);
        ^
    
    TypeError [ERR_PARSE_ARGS_UNKNOWN_OPTION]: Unknown option '--watch'. To specify a positional argument starting with a '-', place it at the end of the mand after '--', as in '-- "--watch"
    
  • 1.0.0-alpha.14+:

    --watch/-w can be omitted, JSON Server 1+ watches for file changes by default
    
  • 1.0.0-alpha.13+, if using Node.js before v18.3.0, v16.17.0:

    import { parseArgs } from 'node:util';
             ^^^^^^^^^
    SyntaxError: The requested module 'node:util' does not provide an export named 'parseArgs'
    

Minimal package file (update version of json-server as needed):

{
  "name": "q77787616",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "start": "json-server --watch db.json"
  },
  "keywords": [],
  "license": "ISC",
  "dependencies": {
    "json-server": "1.0.0-alpha.12"
  }
}

I'm trying to use json-server as follows:

$ json-server --watch db.json

However, I'm getting errors or warnings when I run that mand, depending on the version I have installed:

  • 1.0.0-alpha.1-1.0.0-alpha.12:

    sh: json-server: mand not found
    

    or (on Windows):

    The term 'json-server' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
    At line:1 char:1
    + json-server --watch db.json
    

    or (if executed via npx):

    npm ERR! could not determine executable to run
    
  • 1.0.0-alpha.13:

    node:internal/errors:496
        ErrorCaptureStackTrace(err);
        ^
    
    TypeError [ERR_PARSE_ARGS_UNKNOWN_OPTION]: Unknown option '--watch'. To specify a positional argument starting with a '-', place it at the end of the mand after '--', as in '-- "--watch"
    
  • 1.0.0-alpha.14+:

    --watch/-w can be omitted, JSON Server 1+ watches for file changes by default
    
  • 1.0.0-alpha.13+, if using Node.js before v18.3.0, v16.17.0:

    import { parseArgs } from 'node:util';
             ^^^^^^^^^
    SyntaxError: The requested module 'node:util' does not provide an export named 'parseArgs'
    

Minimal package file (update version of json-server as needed):

{
  "name": "q77787616",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "start": "json-server --watch db.json"
  },
  "keywords": [],
  "license": "ISC",
  "dependencies": {
    "json-server": "1.0.0-alpha.12"
  }
}
Share Improve this question edited Jan 10, 2024 at 9:17 jonrsharpe asked Jan 9, 2024 at 14:48 jonrsharpejonrsharpe 122k30 gold badges268 silver badges475 bronze badges
Add a ment  | 

5 Answers 5

Reset to default 12

json-server is currently in active development towards v1, but unfortunately these alpha versions are being published to npm with the latest tag, so are being installed in favour of the stable version (currently 0.17.4) if you simply npm install json-server. This has caused various issues:

  • Prior to alpha.13 the correct binary wasn't installed at all, so the json-server mand couldn't be found (typicode/json-server#1472).

  • With alpha.13 the binary was included, but the CLI changed such that --watch was an invalid argument (typicode/json-server#1474):

    $ npx [email protected] --help
    Usage: json-server [options] <file>
    Options:
      -p, --port <port>  Port (default: 3000)
      -h, --host <host>  Host (default: localhost)
      -s, --static <dir> Static files directory (multiple allowed)
      --help  Show this message
    
    • The argument parsing is provided by parseArgs from node:util, which was introduced in Node.js v16.17 and v18.3, so earlier versions aren't supported.
  • From alpha.14 onwards, --watch is supported but unnecessary, so the message was reduced to a warning.

You can check which version you currently have installed with:

npm ls json-server

Given the active development and alpha status, the best thing to do currently is to explicitly install the stable version (the documentation for this is still available here):

$ npm install json-server@0

Alternatively, if you want to use the alpha v1 and are using an appropriate Node.js version (i.e. ^16.17 || >=18.3), you can npm install json-server@latest to get the latest version (ensure you have at least alpha.14) and use the mand without the --watch flag:

$ json-server db.json

First , check the installed package version . if you are using version 1.0.0-alpha.23 , there is no need to write --watch . Also , you need to create the db.json file in the root folder of your project . Finally after creating the file , you can use the following mand :

json-server db.json

There’ll be no help updating [email protected] in existing node_modules. Remove existing node_modules folder and install stable version json-server.

npm i [email protected]

Step 1: Open "CMD"

Step 2: npm install -g [email protected]

Step 3: json-server --watch db.json

Sorce: https://github./typicode/json-server/tree/v0

  1. Move the Data File to the Project's Root Folder

The root directory is the main folder of your project, where your package.json file resides.

 project_folder/
 ├── package.json
 ├── cities.json
 ├── (other project files)
  1. Edit package.json file to Add a Server Script . Open the package.json file. . Add a script to start a JSON server.

    "scripts": { "server": "json-server cities.json" }

  2. Run the Server from the Terminal

    npm run server

Once executed, json-server will start and host the cities.json file as an API at a default URL (usually http://localhost:3000).

发布评论

评论列表(0)

  1. 暂无评论