Javascript Node.js在需要JSON时崩溃

Javascript Node.js在需要JSON时崩溃,javascript,json,node.js,parsing,require,Javascript,Json,Node.js,Parsing,Require,我一直在尝试将一些变量移动到一个外部文件,所以我决定创建一个config.json { "username":"...", "password":"...", "identity_secret":"...", "shared_secret":"...", "hostname":"127.0.0.1", "owner":"..." } 然后要求它作为一个模块 'use strict' var ACCEPT = 0xf var REPORT = 0xff var DECL

我一直在尝试将一些变量移动到一个外部文件,所以我决定创建一个config.json

{
  "username":"...",
  "password":"...",
  "identity_secret":"...",
  "shared_secret":"...",
  "hostname":"127.0.0.1",
  "owner":"..."
}
然后要求它作为一个模块

'use strict'

var ACCEPT = 0xf
var REPORT = 0xff
var DECLINE = 0xfff

var fs = require('fs')
var config = require('./config.json')
但是每当我运行脚本时,我总是会遇到这个错误

module.js:457
  throw err;
  ^

Error: Cannot find module 'config.json'
  at Function.Module._resolveFilename (module.js:455:15)
  at Function.Module._load (module.js:403:25)
  at Module.require (module.js:483:17)
  at require (internal/module.js:20:19)
  at Object.<anonymous> (/root/steam-bot/steam_bot.js:8:14)
  at Module._compile (module.js:556:32)
  at Object.Module._extensions..js (module.js:565:10)
  at Module.load (module.js:473:32)
  at tryModuleLoad (module.js:432:12)
  at Function.Module._load (module.js:424:3)

有两个可能的原因,你会得到这个错误。1) JSON文件与脚本不在同一目录中,或者2)您使用的是旧版本的Node.js(0.5之前)。确保主脚本和JSON文件位于同一目录中。另外,如果您不想更新Node.js,您仍然可以包含如下JSON文件:

var imported = JSON.parse(require('fs').readFileSync('.\\config.json') + '');

我浏览了脚本的其余部分,发现正在解析另一个polldata.json,它是由我正在使用的库生成的

出于某种奇怪的原因,这是不恰当的

Error: Cannot find module 'config.json'

您需要json的文件是否在同一目录中?您在
require.js
中给出了一个错误的
config.json
路径,有没有提示如何重现您的问题?这就是问题的全部,由于此require是异步的,下面的任何内容都不应该影响它。请告诉我们有关如何重现您得到的错误的更多信息。他使用的是v6.4.0提到it@UnknownDeveloper我知道,但我也写这篇文章给未来的用户谁得到同样的问题。也许我的答案可以帮助他们。哦,好的,好主意!这两个原因都不是问题所在case@tolunlade如果这两种情况都不是你的,你需要提供更多关于你的问题的信息,并让我们知道如何重现你的错误,否则我不能说更多。
Error: Cannot find module 'config.json'