0

I am trying to read a file from ipfs/internet and then execute it in a meteor-environment. As one cannot place functions in a json-file, I thought I'd try this approach where the function code is separated in two string fields and then put togheter with new Function(arguments, body).

The problem I've encountered is that this...

new Function("message, signedMessage", 'import {elliptic} from "elliptic";')

creates this...

SyntaxError: Unexpected token import

That is, javascript doesn't seem to be able to read Meteor-specific (?) code such as import. It works fine with the node-notation const elliptic = require("elliptic");.

Is it possible to use new Function() to accept code with import-notation into the meteor framework?

Also, any suggestions on how to solve my main problem (getting code from the net into the meteor framework) is appreciated. :-)

Carl Jakobsson
  • 125
  • 1
  • 7
  • 2
    The `import` syntax can only appear at the outer lexical level of the code, not inside functions. (Except for "dynamic" imports, which the above code isn't doing.) – Pointy Jun 02 '19 at 18:28
  • 2
    @Pointy and in Node.js, you can only use `import` by enabling experiment features – r3wt Jun 02 '19 at 18:30
  • 1
    `Also, any suggestions on how to solve my main problem` how much of this foreign code is really required to be injected into your app? Can you please elaborate a bit more on your overall setup? – Jankapunkt Jun 02 '19 at 20:32
  • Strangely enough, I have been able to put these kinds of static imports in any function in my meteor projects - even when I've never placed them in their regular place. Maybe some meteor magic? But thanks for the pointer, @Pointy! I'm looking for a new workaround now. – Carl Jakobsson Jun 02 '19 at 21:07
  • @Jankapunkt, What I want to do is run external code and see if it executes in a certain way. So, let's say we have a `HelloWorld`-greeter with publicKey-controlled access. I want to run it with some external input and see what the results are. – Carl Jakobsson Jun 02 '19 at 21:13
  • 2
    In you Meteor project `import` works, because Meteor builds your code before running and transpiles all the imports down to a node/browser compatible module structure. – Jankapunkt Jun 02 '19 at 22:11

0 Answers0