尝试按照 官方手册 实现模块,我收到此错误消息:
未捕获的 ReferenceError:未定义导出
在 app.js:2
但是在我的代码中,我从未使用过名称 exports
。
我怎样才能解决这个问题?
文件
应用程序.ts
let a = 2;
let b:number = 3;
import Person = require ('./mods/module-1');
模块-1.t
export class Person {
constructor(){
console.log('Person Class');
}
}
export default Person;
tsconfig.json
{
"compilerOptions": {
"module": "commonjs",
"target": "es5",
"noImplicitAny": false,
"sourceMap": true,
"outDir": "scripts/"
},
"exclude": [
"node_modules"
]
}
原文由 George C. 发布,翻译遵循 CC BY-SA 4.0 许可协议
编辑:
如果您不再针对
es5
,此答案可能不起作用,我将尝试使答案更完整。原始答案
如果未安装 CommonJS( 定义
exports
),则必须从tsconfig.json
中删除此行:根据评论,仅此一项可能不适用于
tsc
的更高版本。如果是这种情况,您可以安装一个模块加载器,如 CommonJS、SystemJS 或 RequireJS,然后指定它。笔记:
查看
tsc
生成的main.js
文件。你会在最上面找到这个:它是错误信息的根源,删除
"module": "commonjs",
后,它就会消失。