require is not defined

You’re trying to use a CommonJS module from within your browser.
This will not work.

How are you using them?
When you write import ... from ... in ES6 Babel will transpile these calls to a module definition called CommonJS and since CommonJS isn’t around in the browser you’ll get an undefined error from require().

Furthermore, you’re also trying to load RequireJS which uses a different module definition pattern called AMD, Asynchronous Module Definition, and will not take care of the require calls for you. You can wrap them in RequireJS specific calls.

If you want to use CommonJS modules in your code base you need to first bundle them with either Browserify or webpack. The two tools will transform your require calls to some glue magic that you can use within the browser.

But in your specific case, if you remove the import calls and just let the browser take care of and attach the classes you’ve created to the window object your code should work.

Leave a Comment