Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

What's up with "import declarations may only appear at top level of a module"

4,693 views
Skip to first unread message

Kent Williams

unread,
Jun 12, 2018, 6:06:28 PM6/12/18
to dev-tech-...@lists.mozilla.org
So SpiderMonkey 52 supports ES6, and we're embedding the TypeScript
compiler.  I got that part working but I get an error compiling the
compiled scripts.

"compilation exception: import declarations may only appear at top level
of a module: file: , line: 1"

This is my test program:

import Add from "./TSTest2";
let x:string='Hello from Typscript\n';

print_doc(x + Add(2,2).toString() + '\n');
// cause_an_error;

And this is the module it's trying to import:

export default function Add(x: number, y:number): number {
     return x + y;
}

When I google that error message, all the references to <script> tags in
client side JS.  How do I get a server-side SpiderMonkey interpreter to
not throw that error.


Miles

unread,
Jun 13, 2018, 4:58:22 AM6/13/18
to
Sorry, I don't know the answer to this, but I would be very interested to find out if you manage to get it to work. I'm trying to upgrade our Spidermonkey embedding from 1.8.5 and I have grabbed version 59. I'm very interested to know if/how ES6 features are enabled. Are they by default or do you have to call some functions etc.
If you find the solution please can you post. Many thanks.

Jan de Mooij

unread,
Jun 13, 2018, 12:01:08 PM6/13/18
to Kent Williams, dev-tech-...@lists.mozilla.org
You're probably trying to compile/execute the script as a plain script
instead of a module. See JS::CompileModule. There's also support for
modules in the JS shell (-m module I think), you could look at that as well.

Also, you might want to consider upgrading to ESR 60, its module support is
likely a lot better than what's in 52.

Hope this helps,
Jan

On Tue, Jun 12, 2018 at 3:05 PM, Kent Williams <kwil...@leepfrog.com>
wrote:
> _______________________________________________
> dev-tech-js-engine mailing list
> dev-tech-...@lists.mozilla.org
> https://lists.mozilla.org/listinfo/dev-tech-js-engine
>

Kent Williams

unread,
Jun 18, 2018, 9:55:32 AM6/18/18
to Jan de Mooij, dev-tech-...@lists.mozilla.org
OH and there's nothing about CompileModule in the SpiderMonkey API
documentation.
> <mailto:dev-tech-...@lists.mozilla.org>
> https://lists.mozilla.org/listinfo/dev-tech-js-engine
> <https://lists.mozilla.org/listinfo/dev-tech-js-engine>
>
>

Kent Williams

unread,
Aug 6, 2018, 9:40:03 AM8/6/18
to dev-tech-...@lists.mozilla.org
There's a simple issue: Scripts with anything related to modules require
using JS::CompileModule. Regular JS scripts require using JS::CompileScript

I got onto the #JSAPI IRC channel and with the help of those people I
was able to build something that works.

You're using a newer version of SpiderMonkey than I targeted, so you may
have to do some tweaks.  If you get it working please submit a pull
request to me that shows your changes.

https://github.com/Chaircrusher/SpiderMonkeyES6Example

You've (yet again) become a victim of Mozilla's spotty commitment to
documentation.  I would respectfully submit that having "Ask the actual
developers on IRC" as a documentation strategy is frustrating and flawed.

On 06/13/2018 03:58 AM, Miles wrote:
> Sorry, I don't know the answer to this, but I would be very interested to find out if you manage to get it to work. I'm trying to upgrade our Spidermonkey embedding from 1.8.5 and I have grabbed version 59. I'm very interested to know if/how ES6 features are enabled. Are they by default or do you have to call some functions etc.
> If you find the solution please can you post. Many thanks.
> _______________________________________________
> dev-tech-js-engine mailing list
> dev-tech-...@lists.mozilla.org
> https://lists.mozilla.org/listinfo/dev-tech-js-engine

0 new messages