SyntaxError:位于0位置的JSON中的意外令牌v

时间:2023-01-25 22:30:35

I was about to use live-server and I got this error when I followed the instructions to set up ~/.live-server.json

我正要使用live-server,当我按照说明设置~/.live-server.json时,我得到了这个错误

undefined:1
var liveServer = require("live-server");
^

SyntaxError: Unexpected token v in JSON at position 0
    at JSON.parse (<anonymous>)
    at Object.<anonymous> (/usr/local/lib/node_modules/live-server/live-server.js:20:20)

Does this mean var cannot be contained in a JSON file?

这是否意味着var不能包含在JSON文件中?

2 个解决方案

#1


1  

var liveServer = require("live-server");

It means that you required npm module live-server. You don't assign json to var liveServer.

这意味着您需要npm模块实时服务器。您不会将json赋给var liveServer。

And when you require this module it loads and parse json file ~/.live-server.json

当您需要这个模块时,它会加载并解析json文件~/.live-server.json

Your error means that your json file isn't in correct json format - you have bug there.

您的错误意味着您的json文件不是正确的json格式——您有错误。

Please paste your json to any json validator (for example http://jsonlint.com/) and fix issues.

请将json粘贴到任何json验证器(例如http://jsonlint.com/)并修复问题。

#2


0  

No, you cannot use var in a JSON file and you can't call any function (like require). JSON is just a format to represent data.

不,您不能在JSON文件中使用var,也不能调用任何函数(如require)。JSON只是一种表示数据的格式。

I just looked into the documentation very briefly but as I understand it you can define options in this JSON file that are then used by live-server. In this case you don't have to specify them in your js file. But in every case you have to require and start live-server inside of a JavaScript file.

我只是简要地研究了一下文档,但是据我所知,您可以在这个JSON文件中定义选项,然后由live-server使用。在这种情况下,您不必在js文件中指定它们。但是在任何情况下,您都需要在JavaScript文件中启动服务器。

#1


1  

var liveServer = require("live-server");

It means that you required npm module live-server. You don't assign json to var liveServer.

这意味着您需要npm模块实时服务器。您不会将json赋给var liveServer。

And when you require this module it loads and parse json file ~/.live-server.json

当您需要这个模块时,它会加载并解析json文件~/.live-server.json

Your error means that your json file isn't in correct json format - you have bug there.

您的错误意味着您的json文件不是正确的json格式——您有错误。

Please paste your json to any json validator (for example http://jsonlint.com/) and fix issues.

请将json粘贴到任何json验证器(例如http://jsonlint.com/)并修复问题。

#2


0  

No, you cannot use var in a JSON file and you can't call any function (like require). JSON is just a format to represent data.

不,您不能在JSON文件中使用var,也不能调用任何函数(如require)。JSON只是一种表示数据的格式。

I just looked into the documentation very briefly but as I understand it you can define options in this JSON file that are then used by live-server. In this case you don't have to specify them in your js file. But in every case you have to require and start live-server inside of a JavaScript file.

我只是简要地研究了一下文档,但是据我所知,您可以在这个JSON文件中定义选项,然后由live-server使用。在这种情况下,您不必在js文件中指定它们。但是在任何情况下,您都需要在JavaScript文件中启动服务器。