Skip to content Skip to sidebar Skip to footer

Typescript. ReferenceError: Require Is Not Defined

I read official doc for TypeScript, and I copy code from there. I install commonjs and requerejs. 'devDependencies': { ... 'commonjs': 'latest', 'requirejs': 'latest' } But

Solution 1:

Browsers don't have a understanding of modules (yet). That means while require() works if you execute it in node only, browsers don't know what to do with it.

There are 2 main methods of fixing this:

  • Remove the exports/require statements and include every single js file (in correct dependency order) into your html file (as script tags). They will all have the same scope and will work as if there was just one large concatenated file. This solution is pretty bad tho as it destroys scoping, doesn't really work with modules you got with npm and has bad loading times, as it has to fetch multiple js files from the server.
  • Use a resource bundler like webpack. Webpack will look at all dependant files starting from your main.js file and bundle and compress them down into a single small .js file. This also works for external dependencies (npm). All you have to do then is to include that single bundle.js file into the html.

Post a Comment for "Typescript. ReferenceError: Require Is Not Defined"