需要帮助理解 Node JS 语法错误

Need Help Understanding Node JS Syntax Error

任何人都可以根据下面附带的屏幕截图帮助我理解语法错误吗?

我的脚本应该访问给定的 JSON 和 return 特定值,但不知何故 return 出现此错误。

编辑 1

我用虚拟 JSON 测试了我的脚本,脚本没有 return 任何错误,所以我怀疑我原来的 JSON 可能有问题。这是我的 JSON.

{
   "og_object": {
      "id": "1192199560899293",
      "description": "Hi everyone I have an important announcement to make. So ever since Penny started school, I've been having mixed feelings. \u00a0Besides having a bit of space to myself to breathe and rest my brain/legs, I'm actually a bit bittersweet cos my little baby, who used to sleep at weird hours and gobble puree",
      "title": "Fighter and Penny's new sibling",
      "type": "article",
      "updated_time": "2017-04-12T01:17:57+0000"
   },
   "share": {
      "comment_count": 0,
      "share_count": 109
   },
   "id": "http://fourfeetnine.com/2017/03/05/fighter-and-pennys-new-sibling/"
}

编辑 2

这是我 运行 产生错误的脚本。

var objects = require('./output.txt');
console.log(objects);

output.txt 是包含我在 编辑 1

中粘贴的 JSON 的文件

感谢@Jordan 的建议。故障确实是由于错误的文件扩展名。将文件扩展名从 .txt 更改为 .json 后,语法错误就消失了。

var objects = require('./output.txt');

require() 函数属于文档中显示的 module loading system. Despite the name, it can actually load several types of files and directories and not only Node modules. As per the high-level algorithm in pseudocode

require(X)

If X begins with './' or '/' or '../'

a. LOAD_AS_FILE(Y + X)

[...]

LOAD_AS_FILE(X)

  1. If X is a file, load X as JavaScript text. STOP
  2. If X.js is a file, load X.js as JavaScript text. STOP
  3. If X.json is a file, parse X.json to a JavaScript Object. STOP
  4. If X.node is a file, load X.node as binary addon. STOP

由于您得到 SyntaxError,output.txt 不包含有效的 JavaScript 代码。

如果您真的想加载 JSON,您需要通过将文件重命名为 output.json.

来执行子规则 #3