Visual Studio 2015 Update 2-'中的TypeScript模块;要求'未定义
TypeScript Modules in Visual Studio 2015 Update 2 - 'require' is undefined
在Visual Studio 2015 Enterprise(已安装Update 2)中,我从模板创建了一个新的TypeScript项目TypeScriptHTMLApp1(使用默认项目设置)。之后,我创建了新的TypeScript文件log.ts,并从https://github.com/Microsoft/TypeScript/blob/master/doc/spec.md#11.3到新创建的文件。
// -------- log.ts --------
export function message(s: string) {
console.log(s);
}
接下来,我将以下几行代码复制到app.ts文件的开头。
// -------- app.ts --------
import { message } from "./log";
message("hello");
class Greeter {
element: HTMLElement;
当我尝试在Internet Explorer中(从Microsoft visual Studio IDE内部)运行此项目时,我收到以下错误消息:
Unhandled exception at line2, column 1 in http://localhost:20728/app.js
0x800a1391 - JavaScript runtime error: 'require' is undefined
您需要包含requirej或将脚本与webpack捆绑在一起。TypeScript提供模块语法并向下编译为require
调用,但不提供运行时模块加载程序。您必须将其包含在页面中,脚本才能工作。
在浏览器中,确保提供--module
标志并指定正在使用的模块类型。对于通过webpack运行的代码,这通常是es6
,对于使用requirejs的旧代码,这将是amd
/umd
。
如果你看一下TS编译器的输出,你会发现
import {foo} from './bar';
成为
define(["require", "exports"], function (require, exports) {
"use strict";
});
但请注意,CCD_ 6和模块CCD_ 7和CCD_。
如果您在节点环境中运行,nodejs运行时会为commonjs模块加载提供必要的功能。
相关文章:
- Typescript模块没有导出的成员-react
- Visual Studio 2015 Update 2-'中的TypeScript模块;要求'未定义
- 可以't在不提供文件扩展名的情况下导入TypeScript模块
- Typescript 0.9.1.1模块被拆分为几个.ts文件
- Typescript模块和systemjs.从内联脚本实例化类
- 通过 RequireJS 从 javascript 调用 TypeScript 模块
- jspm / jQuery / TypeScript - 模块“jquery”没有默认导出
- 来自 TypeScript 模块的 JavaScript IIFE 中的默认参数的目的是什么?
- 用requireJS加载一些typescript模块
- 运行时未定义Typescript模块
- 为什么“;这个“;在具有http promise的typescript模块中未定义
- 使用typescript模块进行Browserify -什么是最佳设计实践?
- 从typescript模块中注册Knockout.js组件视图模型
- Typescript模块放到单个JS文件中
- 需要帮助来理解Angular 2/Typescript模块在Prime NG中的声明
- 如何在NodeJS中公开TypeScript模块
- 无法使用——out将typescript模块(.ts)文件连接到单个输出(.js)中
- Typescript模块工厂模式
- 引用嵌套的TypeScript模块
- Typescript模块,需要外部node_modules