【发布时间】:2021-02-24 14:57:10
【问题描述】:
我正在尝试将带有import template as "./template.html" 的html 文件作为字符串导入,以便通过Express 的res.end(template); 函数将其提供给用户。
我在项目的根目录中创建了一个index.d.ts 文件,其中包含以下内容:
declare module '*.html' {
const value: string;
export default value;
}
它让我可以毫无问题地导入template.html,但是当我运行程序时它会说
/home/koen/Workspace/xyz/src/template.html:1
<!DOCTYPE html>
^
SyntaxError: Unexpected token <
at Module._compile (internal/modules/cjs/loader.js:723:23)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:789:10)
at Module.load (internal/modules/cjs/loader.js:653:32)
at tryModuleLoad (internal/modules/cjs/loader.js:593:12)
at Function.Module._load (internal/modules/cjs/loader.js:585:3)
at Module.require (internal/modules/cjs/loader.js:692:17)
at require (internal/modules/cjs/helpers.js:25:18)
at Object.<anonymous> (/home/koen/Workspace/xyz/out/index.js:11:41)
at Module._compile (internal/modules/cjs/loader.js:778:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:789:10)
这几乎就像它试图将 html 作为 Javascript 或其他东西运行一样。任何想法为什么会发生这种情况?我被卡住了……
【问题讨论】:
标签: typescript express import typing