Error AtlasError Documentation and Resolution

JavaScript import declaration is not at the top level of a module

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

JavaScript found an import statement inside a block or a file that is not being treated as a module.

SyntaxError: import declarations may only appear at top level of a modulemodules
SyntaxError: import declarations may only appear at top level of a module
  • An import statement is nested inside a function, conditional, or block.
  • The file is not being executed as a module.
  • Source code with ESM syntax was run before build or transpilation.
  1. Move static import declarations to the top level of the module.
  2. Use dynamic import() for conditional or lazy loading.
  3. Make sure the file is treated as a module in the browser, bundler, or runtime.
MDN: SyntaxError: import declarations may only appear at top level of a module
JavaScript import declarations may only appear at top level of a module | Error Atlas