Client on node: Uncaught ReferenceError: require is not defined

前端 未结 8 1616
遥遥无期
遥遥无期 2020-11-21 07:20

So, I am writing an application with the node/express + jade combo.

I have client.js, which is loaded on the client. In that file I have code that calls

8条回答
  •  庸人自扰
    2020-11-21 07:57

    In my case I used another solution.

    As project doesn't require CommonJs and it must have ES3 compatibility (modules not supported) all you need is just remove all export and import statements from your code, because your tsconfig doesn't contain

    "module": "commonjs"
    

    But use import and export statements in your referenced files

    import { Utils } from "./utils"
    export interface Actions {}
    

    Final generated code will always have(at least for typescript 3.0) such lines

    "use strict";
    exports.__esModule = true;
    var utils_1 = require("./utils");
    ....
    utils_1.Utils.doSomething();
    

提交回复
热议问题