我正在尝试使用 json-server
,命令如下:
$ json-server --watch db.json
但在执行该命令时,根据安装的版本不同,遇到了一些错误或警告:
对于 1.0.0-alpha.1
到 1.0.0-alpha.12
版本:
- 在 Unix/Linux/macOS 系统中:
sh: json-server: command not found
- 在 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
- 如果通过
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 command 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
及以上版本,如果使用的 Node.js 版本早于 v18.3.0 或 v16.17.0:
+, if using Node.js before v18.3.0, v16.17.0:</p>
<pre class="lang-none prettyprint-override"><code>import { parseArgs } from 'node:util';
^^^^^^^^^
SyntaxError: The requested module 'node:util' does not provide an export named 'parseArgs'
最小化的 package.json
文件(根据需要更新 json-server
的版本)如下:
{
"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"
}
}