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

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.


0
Kent
6/12/2018 10:05:52 PM
mozilla.dev.tech.js-engine 2024 articles. 0 followers. Post Follow

2 Replies
101 Views

Similar Articles

[PageSpeed] 48

Sorry, I don't know the answer to this, but I would be very interested to f=
ind out if you manage to get it to work. I'm trying to upgrade our Spidermo=
nkey embedding from 1.8.5 and I have grabbed version 59. I'm very intereste=
d to know if/how ES6 features are enabled. Are they by default or do you ha=
ve to call some functions etc.
If you find the solution please can you post. Many thanks.
0
Miles
6/13/2018 8:58:21 AM
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-js-engine@lists.mozilla.org
> https://lists.mozilla.org/listinfo/dev-tech-js-engine

0
Kent
8/6/2018 1:39:42 PM
Reply: