【问题标题】:CommonJS Require Error : SyntaxError: Unexpected token <CommonJS 需要错误:SyntaxError: Unexpected token <
【发布时间】:2016-06-06 07:39:41
【问题描述】:

我想要求我的 html,但我做不到。我的代码很简单:

module.exports = {
  html: require('./template.html')
};

当我运行代码时,它给出了这个错误:

SyntaxError: Unexpected token <
    at exports.runInThisContext (vm.js:53:16)
    at Module._compile (module.js:414:25)
    at Object.Module._extensions..js (module.js:442:10)
    at Module.load (module.js:356:32)
    at Function.Module._load (module.js:313:12)
    at Module.require (module.js:366:17)
    require (module.js:385:17)
    at Object.<anonymous> (D:\frontend\lokidump\server\views\traders\betofcourse\upcomingEvents\index.js:2:9)

【问题讨论】:

    标签: node.js require commonjs


    【解决方案1】:

    当您在文件上使用require 时,它需要的是 JavaScript 文件,而不是 HTML 文件。如果要将 HTML 文件的内容加载到变量中,请查看 fs.readFile

    【讨论】:

    • 嗨@JDelaney,所以这可以解决我的问题吗? html : fs.readFile('./template.html',function(err,html){return html;})
    • @KamuranSönecek 您将不得不改用fs.readFileSync。直接返回文件内容,不使用回调
    猜你喜欢
    • 2015-04-21
    • 2016-07-22
    • 1970-01-01
    • 2021-09-17
    • 1970-01-01
    • 2016-10-04
    • 2016-07-28
    • 2014-04-07
    • 2023-03-04
    相关资源
    最近更新 更多