TypeScript Modules in Visual Studio 2015 Update 2 - 'require' is undefined

后端 未结 1 2005
梦如初夏
梦如初夏 2021-01-27 15:09

In Visual Studio 2015 Enterprise (Update 2 installed) I\'ve created a new TypeScript-Project TypeScriptHTMLApp1 from the Template (using default project settings). After that, I

相关标签:
1条回答
  • 2021-01-27 15:45

    You need to include requirejs or bundle your scripts with webpack. TypeScript provides the module syntax and compiles down into require calls, but does not provide a runtime module loader. You must include that in your page for the scripts to work.

    In the browser, make sure to provide the --module flag and specify the type of module you're using. This will typically be es6 for code you're running through webpack or amd/umd for older code using requirejs.

    If you take a look at the TS compiler's output, you'll see that

    import {foo} from './bar';
    

    becomes

    define(["require", "exports"], function (require, exports) {
        "use strict";
    });
    

    but note that define and the modules "require" and "exports" are not defined by the compiler.

    If you're running in a node environment, the nodejs runtime provides the necessary functions for commonjs module loading.

    0 讨论(0)
提交回复
热议问题