require is not defined

前端 未结 1 1174
小蘑菇
小蘑菇 2020-11-30 02:28

Im building a new React app but get the following error - \"require is not defined\"

hello-world.html



  

        
相关标签:
1条回答
  • 2020-11-30 02:54

    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.

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