【发布时间】:2018-03-02 03:11:56
【问题描述】:
我正在编写一个导入 JSON 文件的 node.js 模块:
const distDirPath = "c:/temp/dist/";
const targetPagePath = "c:/temp/index.html";
const cliJsonPath = "C:/CODE/MyApp/.angular-cli.json";
const fs = require('fs');
function deployAot()
{
var version = JSON.parse(fs.readFileSync(cliJsonPath, 'utf8')).version;
}
// export the module
module.exports = {
DeployAot: deployAot
};
我在https://jsonlint.com/ 中验证了上面 json 文件的内容,它是有效的 json,但是 deployAot() 中的第一行代码在我执行节点模块时返回以下错误:
"Unexpected token in JSON at position 0"
这是具体的json:
https://jsonblob.com/cd6753d2-9e51-11e7-aa97-2f95b001b178
知道这里可能存在什么问题吗?
【问题讨论】:
-
也许给我们看一个 JSON 本身?
-
字节序标记?
-
尝试将您的 JSON 简化为 1 个键值对。然后打印
fs.readFileSync(cliJsonPath, 'utf8')的结果,看看字符串开头有没有奇怪的字符。 -
@LEQADA 我用 json 的 url 更新了我的原始帖子
-
我说的是你在解析后缺少
project键