1

When I use require I get ReferenceError: require is not defined and when I use import/export I get SyntaxError: Cannot use import statement outside a module.

Is it maybe because I am trying to use this commands on JavaScript file that is connected to HTML file? Because when I use this commands in my server.js file (that is not connected to any HTML file) I can normally use require.

If so, how can I overcome this problem. I got JavaScript file connected with each HTML page and I want to import functions to thoose scripts to make code clearer.

Donald Duck
  • 8,409
  • 22
  • 75
  • 99
vturkovic
  • 117
  • 2
  • 8
  • use tag script with src in html – Daniil Loban Dec 17 '20 at 17:13
  • @vturkovic can you show a little piece of what you do in html - why do you need import or require. Best way - keep separately html and js – Daniil Loban Dec 17 '20 at 17:19
  • There are some rules that you must follow when using them in a web browser, this might help you ["Uncaught SyntaxError: Cannot use import statement outside a module" when importing ECMAScript 6](https://stackoverflow.com/questions/58211880/uncaught-syntaxerror-cannot-use-import-statement-outside-a-module-when-import) – Vinay Dec 17 '20 at 17:37
  • As far as I know Javascript doesn't have import, export or require. You need to add `` to your HTML. – Donald Duck Dec 18 '20 at 11:17

1 Answers1

0

as far as I know import and export only work with modules. in order to overcome that, you can use Webpack for that, which you can basically use export and import between files, and webpack bundles them all together in one file. this is a tutorial for beginners on how to set it up if you are interested: https://www.youtube.com/watch?v=TzdEpgONurw&ab_channel=DesignCourse

sham
  • 71
  • 4