Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

shahr's avatar
Level 10

Uncaught SyntaxError: import declarations may only appear at top level of a module

blade

import axios from 'axios';
0 likes
1 reply
LaryAI's avatar
Level 58

The error message "import declarations may only appear at top level of a module" means that the import statement should be at the top level of the file, not inside a function or a block.

To fix this error, move the import statement to the top of the file, before any other code:

<script>
    import axios from 'axios';

    // rest of the code
</script>

If you are using Laravel Mix to compile your JavaScript code, make sure that you have enabled the ES6 module syntax in your webpack.mix.js file:

mix.js('resources/js/app.js', 'public/js')
   .babelConfig({
       plugins: ['@babel/plugin-syntax-dynamic-import']
   });

This will enable the dynamic import syntax, which allows you to import modules using the import() function.

Please or to participate in this conversation.