尝试实现遵循official handbook的模块时,我收到以下错误消息:
未捕获ReferenceError:未定义导出
在app.js:2
但是在我的代码中,我从来没有使用过exports
这个名字。
我该如何解决这个问题呢?
文件
app.ts
let a = 2;
let b:number = 3;
import Person = require ('./mods/module-1');
module-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"
]
}
发布于 2017-05-16 18:34:18
编辑:
如果你不再以es5
为目标,这个答案可能不起作用,我会试着让答案更完整。
原始答案
如果未安装CommonJS (which defines exports
),则必须从tsconfig.json
中删除此行
"module": "commonjs",
根据评论,这可能不适用于更高版本的tsc
。如果是这种情况,您可以安装一个模块加载器,如CommonJS、SystemJS或RequireJS,然后指定它。
备注:
看看tsc
生成的main.js
文件。你会在最上面找到这个:
Object.defineProperty(exports, "__esModule", { value: true });
它是错误消息的根源,在删除"module": "commonjs",
之后,它将消失。
发布于 2017-10-23 18:47:28
这个问题的其他解决方案很少
<script>var exports = {};</script>
发布于 2020-04-03 02:33:08
一种解决方案:
从package.json
中删除"type": "module"
。
https://stackoverflow.com/questions/43042889
复制相似问题