jon*_*rpe 3 javascript npm json-server
我尝试使用json-server如下:
$ json-server --watch db.json
Run Code Online (Sandbox Code Playgroud)
但是,当我运行该命令时,我收到错误或警告,具体取决于我安装的版本:
1.0.0-alpha.1- 1.0.0-alpha.12:
sh: json-server: command not found
Run Code Online (Sandbox Code Playgroud)
或(在 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
Run Code Online (Sandbox Code Playgroud)
或(如果通过执行npx):
npm ERR! could not determine executable to run
Run Code Online (Sandbox Code Playgroud)
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"
Run Code Online (Sandbox Code Playgroud)
1.0.0-alpha.14+:
--watch/-w can be omitted, JSON Server 1+ watches for file changes by default
Run Code Online (Sandbox Code Playgroud)
1.0.0-alpha.13+,如果使用 v18.3.0、v16.17.0 之前的 Node.js:
import { parseArgs } from 'node:util';
^^^^^^^^^
SyntaxError: The requested module 'node:util' does not provide an export named 'parseArgs'
Run Code Online (Sandbox Code Playgroud)
最小包文件(根据需要更新版本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"
}
}
Run Code Online (Sandbox Code Playgroud)
json-server目前正在积极开发 v1 版本,但不幸的是,这些 alpha 版本正在使用标签发布到 npm latest,因此如果您只是简单地安装,则可以选择稳定版本(当前0.17.4)npm install json-server。这导致了各种问题:
之前alpha.13根本没有安装正确的二进制文件,因此json-server无法找到该命令 ( typicode/json-server#1472)。
包含alpha.13了二进制文件,但 CLI 发生了更改,导致--watch参数无效 ( typicode/json-server#1474):
$ npx json-server@1.0.0-alpha.13 --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
Run Code Online (Sandbox Code Playgroud)
parseArgsfromnode:util提供,它是在 Node.js v16.17 和 v18.3 中引入的,因此不支持早期版本。从alpha.14开始,--watch支持但不必要,因此该消息被简化为警告。
您可以检查当前安装的版本:
npm ls json-server
Run Code Online (Sandbox Code Playgroud)
鉴于活跃的开发和 alpha 状态,当前最好的做法是显式安装稳定版本(此处仍然提供此文档):
npm ls json-server
Run Code Online (Sandbox Code Playgroud)
或者,如果您想使用 alpha v1 并使用适当的 Node.js 版本(即^16.17 || >=18.3),您可以npm install json-server@latest获取最新版本(确保您至少有)并使用不带标志的alpha.14命令:--watch
$ npm install json-server@0
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3796 次 |
| 最近记录: |