question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

export declarations may only appear at top level of a module

See original GitHub issue

I’m facing an issue that seems very similar to #328 however it is occurring at runtime. I’m trying to add Routify to an existing application and getting the following error:

Firefox

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

Brave

Uncaught SyntaxError: Unexpected token 'export'

I’ve gone through the documentation and changed my rollup.config.js output.format to esm as well as using output.dir instead of output.file.

I also found this similar issue in Sapper repo but it was closed without any comment/resolution.


app.ts

import App from './App.svelte';

const app = new App({
  target: document.body
});

export default app;

App.svelte

<script>
  import { Router } from '@roxy/routify';
  import { routes } from '../.routify/routes';
</script>

<Router {routes} />

rollup.config.js

import svelte from 'rollup-plugin-svelte';
import sveltePreprocess from 'svelte-preprocess';
import css from 'rollup-plugin-css-only';
import typescript from '@rollup/plugin-typescript';
import commonjs from '@rollup/plugin-commonjs';
import resolve from '@rollup/plugin-node-resolve';
import livereload from 'rollup-plugin-livereload';

const production = !process.env.ROLLUP_WATCH;

var serve = function() {
  let server;

  var toExit = function() {
    if (server) {
      server.kill(0);
    }
  }

  return {
    writeBundle() {
      if (server) {
        return;
      }

      server = require('child_process').spawn('npm', ['run', 'start', '--', '--dev'], {
        stdio: ['ignore', 'inherit', 'inherit'],
        shell: true
      });

      process.on('SIGTERM', toExit);
      process.on('exit', toExit);
    }
  }
}

export default {
  input: 'src/app.ts',
  output: {
    name: 'app',
    sourcemap: true,
    format: 'esm',
    dir: 'public/build'
  },
  plugins: [
    svelte({
      preprocess: sveltePreprocess({
        sourceMap: !production
      }),
      compilerOptions: {
        dev: !production
      }
    }),

    css({
      output: 'app.css'
    }),

    resolve({
      browser: true,
      dedupe: ['svelte']
    }),

    commonjs(),

    typescript({
      sourceMap: !production,
      inlineSources: !production
    }),

    !production && serve(),

    !production && livereload('public')
  ]
}

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
rpf3commented, Mar 10, 2021

I’ve discovered the issue is related to how the script is being loaded and I’ve opened the following PR on the docs to hopefully help anyone else in the future.

https://github.com/roxiness/routify.dev/pull/224

Read more comments on GitHub >

github_iconTop Results From Across the Web

SyntaxError: import declarations may only appear at top level ...
If I look in my browser debugger I see an error: "SyntaxError: import declarations may only appear at top level of a module"....
Read more >
"export declarations may only appear at top level of a module ...
Describe the bug I am able to extract and compile without any problems, however using the @lingui/loader to load catalogs isn't working.
Read more >
import declarations may only appear at top level of a module
User Agent: Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:67.0) Gecko/20100101 Firefox/67.0. Steps to reproduce: First get "jsshell" from here:.
Read more >
export declarations may only appear at top level of a module
The above line rectifies the syntaxError: Import declarations may only appear at top level of module. It's because when you import, the file...
Read more >
import declarations may only appear at top level of a module ...
JavaScript : SyntaxError: import declarations may only appear at top level of a module [ Gift : Animated Search Engine ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found