【问题标题】:Getting an error when I run NPM live-server运行 NPM live-server 时出错
【发布时间】:2015-12-09 15:45:13
【问题描述】:

这是我正在使用的服务器,https://www.npmjs.com/package/live-server。但是,当我尝试使用~/.live-server.json作为配置文件时,总是失败...这是我在文件中的内容,非常简单。

{
   port: "8001"
}

然后我运行live-server时出现此错误

undefined:2
   port: "8001"
   ^    

SyntaxError: Unexpected token p
    at Object.parse (native)
    at Object.<anonymous> (/usr/local/lib/node_modules/live-server/live-server.js:17:20)
    at Module._compile (module.js:435:26)
    at Object.Module._extensions..js (module.js:442:10)
    at Module.load (module.js:356:32)
    at Function.Module._load (module.js:311:12)
    at Function.Module.runMain (module.js:467:10)
    at startup (node.js:136:18)
    at node.js:963:3

我不知道为什么会这样。

【问题讨论】:

    标签: node.js npm npm-live-server


    【解决方案1】:

    必须引用 JSON 中的属性名称(JSON 不是 JavaScript):

    {
       "port": "8001"
    }
    

    【讨论】:

    • 谢谢!我试过“端口”:8001,但不是两个。
    • { "port": 8001 } 是有效的 JSON。区别在于8001 是一个数字值,"8001" 是一个字符串值,就像在 JavaScript 中一样。
    【解决方案2】:

    应该是:

    {
        "port": 8001
    }
    

    补充说明:嗯,在 JSON 中,name 应该总是双引号,value 也应该双引号,除了数字(如果你双引号一个数字,它变成一个字符串)。像这样:

    {
        "name1": "stringValue",
        "name2": aNumber
    }
    

    【讨论】:

    • 添加更多细节...可能是解释
    猜你喜欢
    • 2016-12-14
    • 1970-01-01
    • 1970-01-01
    • 2020-12-27
    • 2018-11-22
    • 2015-09-02
    • 1970-01-01
    • 2021-07-02
    相关资源
    最近更新 更多