[英]Typescript - ReferenceError: exports is not defined
在我的控制台中,我不断收到ReferenceError: exports is not defined
为错误。
码:
app.ts:
import {IDetails} from "./interfaces/IDetails";
class MyClass implements IDetails{
constructor(){}
public render (elem: string, text: string) {
let el: HTMLElement = document.querySelector(elem);
el.textContent = text;
el.innerText = text;
}
}
window.onload = () => {
let myClass = new MyClass();
myClass.render('body', 'Hello World!');
};
IDetails.ts:
export interface IDetails {
elem: string,
text: string
}
tsconfig.json:
{
"compilerOptions": {
"outDir": "./build/",
"sourceMap": true,
"noImplicitAny": false,
"module": "commonjs",
"target": "es5",
"removeComments": true,
"allowJs": true
}
}
webpack.config.js:
module.exports = {
entry: './index.ts',
output: {
filename: 'bundle.js',
path: __dirname
},
watch: true,
module: {
rules: [
{
test: /\.tsx?$/,
loader: "ts-loader",
options: {
transpileOnly: true
}
}
]
},
resolve: {
extensions: [".tsx", ".ts", ".js"]
},
};
我在这里做错了什么?
编辑 :我已经使用webpack.config.js
和tsconfig.json
编辑了我的问题。 还可能需要注意的是,我是在Chrome浏览器中直接从Webstorm查看文件的。 这可能是个问题吗?
谢谢。
如果要导出CommonJS模块,则可能与您的Typescript配置选项有关。 您可以通过一个名为Browserify的命令行实用程序运行捆绑软件来解决此问题。 参见http://thejsguy.com/javascript/browserify/2015/01/05/browserify-getting-started.html
尝试设置“ allowJs”:false
或者使用排除https://www.typescriptlang.org/docs/handbook/tsconfig-json.html从typescript编译器中排除webpack配置文件
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.