typescript 未捕获的 ReferenceError:未定义导出且需要
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/34895737/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Uncaught ReferenceError: exports is not defined and require
提问by IamStalker
I'm using angularjs and typescript to create some app, I'm having a trouble with this error that I can't solve
我正在使用 angularjs 和 typescript 创建一些应用程序,但我遇到了无法解决的错误
Here is my *.ts code
这是我的 *.ts 代码
export var NgApp = new application.Startup();
///<reference path="../../../../../typings/tsd.d.ts"/>
import {NgApp} from "../../bootstrap";
module views.components.home {
export class HomeComponent {
public constructor() {
}
public static factory():Function[] {
return [
() => new HomeComponent()
]
}
}
}
NgApp.registerComponents(views.components.home,() => this.app.controller);
Here is my GULP task
这是我的 GULP 任务
let tsResult = gulp.src('src/**/*.ts').pipe(ts({
module: 'commonjs',
sourceMap: true
}));
let taskTs = tsResult.js.pipe(gulp.dest('built/'));
This is my error: Uncaught ReferenceError: exports is not defined
这是我的错误:未捕获的 ReferenceError:未定义导出
The question is: How can I use import like es6 in typescript? What I am missing?
问题是:如何在打字稿中使用像 es6 这样的导入?我缺少什么?
回答by KShewengger
I use angular 1.6 and webpack, it works on my side when i changed the module to "umd"
我使用 angular 1.6 和 webpack,当我将模块更改为“umd”时,它在我这边工作
UMD: Universal Module Definition This has brought about the push for a “universal” pattern that supports both styles (AMD and CommonJS)
reference: http://davidbcalhoun.com/2014/what-is-amd-commonjs-and-umd/
UMD:通用模块定义这推动了支持两种风格(AMD 和 CommonJS)的“通用”模式
参考:http: //davidbcalhoun.com/2014/what-is-amd-commonjs-and-umd/
pasted here is my tsconfig.json after I changed it, I run $ tsc
这里粘贴的是我更改后的 tsconfig.json,我运行 $ tsc
{
"compilerOptions": {
"target": "es5",
"module": "umd",
"moduleResolution": "node",
"sourceMap": true,
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"lib": [ "es2015", "dom" ],
"noImplicitAny": true,
"removeComments": true,
"preserveConstEnums": true,
"allowUnreachableCode": true
},
"exclude": [
"node_modules"
]
}
then my "exports is not defined"error is gone.
然后我的“未定义导出”错误消失了。
回答by Umesh Shende
click here"Solved finally" i was also getting same error i changed module:"commonjs" to "module": "es6", because targeting ES5 remove the import/export statements, so these tools cannot remove unused exports.
单击此处“最终解决”我也遇到了同样的错误,我将模块:“commonjs”更改为“模块”:“es6”,因为针对 ES5 删除了导入/导出语句,因此这些工具无法删除未使用的导出。
{
"compilerOptions": {
"target": "es5",
"module": "es6",
"moduleResolution": "node",
"sourceMap": true,
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"lib": [ "es2015", "dom" ],
"noImplicitAny": true,
"suppressImplicitAnyIndexErrors": true
}
}
回答by basarat
How can I use import like es6 in typescript? What I am missing?
如何在打字稿中使用像 es6 这样的导入?我缺少什么?
You need to use a module loader. Here is a sample that uses webpack : https://github.com/AngularClass/angular2-webpack-starter
您需要使用模块加载器。这是一个使用 webpack 的示例:https: //github.com/AngularClass/angular2-webpack-starter