使用节点require和绝对路径加载JSON文件失败

时间:2014-11-10 18:11:42

标签: javascript json node.js sails.js

我尝试使用require来申请JSON文件。但是尽管文件确实存在,但它失败了。我想知道它是否是因为我尝试加载JSON文件而不是像往常那样在那里加载JS文件。

error: verbose: Evaluating c:\wamp\www\Ayolan\Translation\config/locales/languages.json as module "c:\wamp\www\Ayolan\Translation\config/locales/languages.json" failed with error: SyntaxError: Unexpected token :

它似乎不喜欢Windows路径中的:字符。我有什么解决方案?我无法使用文件的相对路径,因为它是一个加载依赖于应用程序配置的文件的共享文件。

var pathLanguages = __config.path.base + __config.path.languages;
var languages = require(pathLanguages + 'languages.json');

我相信使用这种方法加载JS文件会有效。 (我刚刚完成了测试,它确实与JS文件一起工作)

使用节点0.10.24

JSON文件:

{
    "fr": {
        "min": "fr",
        "lang": "fr-fr",
        "language": "Français"
    },
    "en": {
        "min": "en",
        "lang": "en-en",
        "language": "English"
    }
}

错误讯息:

error: Error: Evaluating c:\wamp\www\Ayolan\Translation\config/locales/fr.json as module "c:\wamp\www\Ayolan\Translation\config/locales/fr.json" failed with error: SyntaxError: Unexpected token :
    at Function.req.load (c:\wamp\www\Ayolan\Translation\node_modules\requirejs\bin\r.js:2505:23)
    at Function.req.get (c:\wamp\www\Ayolan\Translation\node_modules\requirejs\bin\r.js:2455:29)
    at localRequire (c:\wamp\www\Ayolan\Translation\node_modules\requirejs\bin\r.js:1632:40)
    at c:\wamp\www\Ayolan\Translation\shared\app\Lang.js:36:48
    at forOwn (c:\wamp\www\Ayolan\Translation\node_modules\lodash\dist\lodash.js:2105:15)
    at Function.forEach (c:\wamp\www\Ayolan\Translation\node_modules\lodash\dist\lodash.js:3302:9)
    at Function.Lang._loadAllLanguages (c:\wamp\www\Ayolan\Translation\shared\app\Lang.js:34:15)
    at Object.module.exports.bootstrap (c:\wamp\www\Ayolan\Translation\config\bootstrap.js:28:12)
    at Sails.runBootstrap (c:\wamp\www\Ayolan\Translation\node_modules\sails\lib\app\private\bootstrap.js:46:27)
    at Sails.bound [as runBootstrap] (c:\wamp\www\Ayolan\Translation\node_modules\lodash\dist\lodash.js:729:21) { [Error: Evaluating c:\wamp\www\Ayolan\Translation\config/locales/fr.json as module "c:\wamp\www\Ayolan\Translation\config/locales/fr.json" failed with error: SyntaxError: Unexpected token :]
  originalError: [SyntaxError: Unexpected token :],
  moduleName: 'c:\\wamp\\www\\Ayolan\\Translation\\config/locales/fr.json',
  requireModules: [ 'c:\\wamp\\www\\Ayolan\\Translation\\config/locales/fr.json' ],
  fileName: 'c:\\wamp\\www\\Ayolan\\Translation\\config/locales/fr.json' }

0 个答案:

没有答案